6

我正在尝试使用 BeautifulSoup 从页面中获取结果:

req_url = 'http://www.xscores.com/soccer/livescores/25-02'
request = requests.get(req_url)
content = request.content
soup = BeautifulSoup(content, "html.parser")
scores = soup.find_all('tr', {'style': 'height:18px;'}, limit=None)
print(len(scores))
>50

我阅读了这个先前的解决方案:Beautiful Soup findAll 没有找到它们 ,我尝试了 html.parser、lxml 和 html5lib,但没有一个返回超过 50 个结果。有什么建议么?

谢谢

4

3 回答 3

2

尝试使用css-selector查询。

scores = soup.select('#scoretable > tr[style*="height:18px;"]')
print(len(scores))

>>>613
于 2017-02-27T09:41:26.543 回答
2

尝试这个 -

req_url = 'http://www.xscores.com/soccer/livescores/25-02'
request = requests.get(req_url)
html=request.text
soup = BeautifulSoup(html, "html5lib")
scoretable=soup.find('tbody',id='scoretable')
scores=scoretable.find_all('tr')
len(scores)
>617
于 2017-02-27T09:41:33.123 回答
2

此行仅查找具有'height:18px; 的行 风格。

scores = soup.find_all('tr', {'style': 'height:18px;'}, limit=None)

如果您查看页面源并搜索,"height:18px;"您将看到 50 个匹配项。但是,如果您不使用引号进行搜索,height:18px;您将看到 613 个匹配项。

您需要编辑该行以查找具有height:18px;的行。样式(和其他值)。您可以根据文档将正则表达式作为样式参数传递,可能是这样的:

soup.find_all('tr', style = re.compile('height:18px'), limit=None)
于 2017-02-27T09:49:38.097 回答