如何使用BeautifulSoup在div内抓取代码? [英] How can I scrape code inside div with BeautifulSoup?

查看:104
本文介绍了如何使用BeautifulSoup在div内抓取代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用BeautifulSoup和python在div内(不在它们之间)抓取代码时遇到问题.下面,我写了一个html代码,我要剪贴(data-friendscount,data-followerscount值)

I am having problems scraping code inside div not between them, with BeautifulSoup and python. Below I wrote a html code what I want to scrap (data-friendscount , data-followerscount Values)

<div data-profileuserid="285904056" data-friendscount="100" data-followerscount="7102" data-followingscount="25" data-arefriends="false" class="hidden ng-isolate-scope"></div>

推荐答案

toc = requests.get(f'roblox.com/users/75790059/profile')
soup = BeautifulSoup(toc.content, 'html.parser')
divs = soup.find_all("div", class_="hidden ng-isolate-scope")
for div in divs:
    print(div.attrs.get('data-profileuserid', None))
    print(div.attrs.get('data-friendscount', None))
    print(div.attrs.get('data-followerscount', None))
    print(div.attrs.get('data-followingscount', None))
    print(div.attrs.get('data-arefriends', None))

这篇关于如何使用BeautifulSoup在div内抓取代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆