使用python beautifulsoup进行Web爬行

时间:2016-03-04 08:45:20

标签: python html beautifulsoup

如何提取<p>段落标记内的数据以及<li>类下的<div>

1 个答案:

答案 0 :(得分:3)

使用功能find()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)