使用python beautifulsoup进行Web爬网

2024-05-02 05:17:38 发布

您现在位置:Python中文网/ 问答频道 /正文

如何提取<p>段落标记和<li>内的数据,它们位于命名的<div>类下?在


Tags: 数据divli命名段落标记
1条回答
网友
1楼 · 发布于 2024-05-02 05:17:38

使用函数^{}^{}

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)

相关问题 更多 >