正在尝试分析选项卡中的表中的表

2024-03-29 11:27:06 发布

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

我正在分析这个地址:

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

有谁能告诉我我做错了什么,给我指出正确的方向吗?你知道吗


Tags: importurlrequestpagetablecontentfindurllib
1条回答
网友
1楼 · 发布于 2024-03-29 11:27:06

如果有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

相关问题 更多 >