如何使用BeautifulSoup4从HTML表中提取所有项?

2024-10-16 22:25:40 发布

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

这是我正在抓取的一个更大网站的HTML部分。https://pastebin.com/LZ3mJKv0

基本上,我希望我的输出是:

Breed: Shih Tzu
Price: $850
Gender: Male
Nickname: Wade
Age: 16 Weeks Old
Color/Markings: red and white
Size at Maturity: Small

等等,等等。我试着找到所有的tr标签,所有的td标签,和所有的b标签,但没有一个给出我要寻找的输出或给出一个错误

提前感谢您的回复


Tags: httpscom网站htmlnickname标签genderprice
1条回答
网友
1楼 · 发布于 2024-10-16 22:25:40

您可以使用嵌套列表:

from bs4 import BeautifulSoup as soup
d = soup(content, 'html.parser')
new_results = [[c.text.replace('\n', '') for c in i.find_all('td')] for i in d.find_all('tr')]
for i in new_results:
  print(' '.join(i))

输出:

Breed: Shih Tzu
Price: $850
Gender: Male Male
Nickname: Wade
Age: 16 Weeks Old
Color/Markings: red and white
Size at Maturity: Small
Availability Date: 08/01/2018
Shipping Area: Pick Up Only
Payment Method: Credit Cards, Cash

相关问题 更多 >