Skip to main content
 首页 » 编程设计

python之试图在表中的表中解析表

2024年11月01日9hnrainll

我正在尝试解析这个地址:

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) 

我几乎尝试了 table、find 和 findAll 的所有组合,我想要的只是让它吐出最左边的表格像

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

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

请您参考如下方法:

如果有一些 id,class,就使用它。但是在给定的url中,没有有用的id,class。

使用文本文本如下:

>>> 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