使用python beautifulsoup进行Web爬网 [英] Web crawling using python beautifulsoup

查看:68
本文介绍了使用python beautifulsoup进行Web爬网的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何提取位于< div> < p> 段落标签和< li> 中的数据>上课?

How to extract data that is inside <p> paragraph tags and <li> which are under a named <div> class?

推荐答案

使用功能 find_all() :

Use the functions find() and find_all():

import requests
from bs4 import BeautifulSoup

url = '...'

r = requests.get(url)
data = r.text
soup = BeautifulSoup(data, 'html.parser')

div = soup.find('div', {'class':'class-name'})
ps = div.find_all('p')
lis = div.find_all('li')

# print the content of all <p> tags
for p in ps:
    print(p.text)

# print the content of all <li> tags
for li in lis:
    print(li.text)

这篇关于使用python beautifulsoup进行Web爬网的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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