试图解析表中的表内的表

时间:2014-01-21 04:41:16

标签: 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

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

1 个答案:

答案 0 :(得分:2)

如果有idclass,请使用它。但是在给定的网址中,没有任何有用的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