python - 尝试解析表中的表

标签 python web-scraping beautifulsoup html-parsing python-3.3

我正在尝试解析此地址:

LINK

使用此代码并更改此代码:

import urllib
import urllib.request
from bs4 import BeautifulSoup

url=('http://www.bricklink.com/catalogPriceGuide.asp?P=3005&colorID=1&viewExclude=N&v=P')
page = urllib.request.urlopen(url)
soup = BeautifulSoup(page.read())
content = soup.find('table')
price=content.findAll('td')

print(price)

我几乎尝试了 tablefindfindAll 的所有组合,我想要的只是让它吐出最左边的表格类似的东西

Times Sold: 2958
Total Qty:  130610
Min Price:  $0.0136
Avg Price:  $0.0690
Qty Avg Price:  $0.0659
Max Price:  $0.3900

谁能告诉我我做错了什么并指出我正确的方向?

最佳答案

如果有一些idclass,请使用它。但在给定的 url 中,没有有用的 idclass

使用文本文本如下:

>>> import re
>>> import urllib.request
>>>
>>> from bs4 import BeautifulSoup
>>>
>>>
>>> url = 'http://www.bricklink.com/catalogPriceGuide.asp?P=3005&colorID=1&viewExclude=N&v=P'
>>> page = urllib.request.urlopen(url)
>>> soup = BeautifulSoup(page.read())
>>> td = soup.find('td', text=re.compile('Times Sold'))
>>> tr_list = td.parent.parent.find_all('tr')
>>> for tr in tr_list:
...     print(' '.join(td.text for td in tr.find_all('td')))
...
Times Sold: 2958
Total Qty: 130610
Min Price: $0.01
Avg Price: $0.07
Qty Avg Price: $0.07
Max Price: $0.39

关于python - 尝试解析表中的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21249320/

相关文章:

python - 刮痧用美汤和 Selenium 问题

python - BeautifulSoup 没有正确读取文档

python - 在 Selenium Python 中获取 URL

python - Tripadvisor 抓取 'moreLink'

python - 将 html 表转换为字典而不丢失结构

python - 根据文件名在 python 中读取多个图像

python - 从 Selenium 导入 webdriver ModuleNotFoundError : No module named 'selenium' (PyCharm)

python - 从子进程实时捕获标准输出和相机帧

python - Julia:使用 EllipsisNotation 在某些维度上添加元素明智的操作

python - 当代码命中缺失值时如何修复 Web 抓取 Python 代码 "IndexError: list index out of range"