【问题标题】:Getting a return of [] when using BS4 on a webpage在网页上使用 BS4 时返回 []
【发布时间】:2019-08-14 18:39:36
【问题描述】:

我正在尝试使用 bs4 从网站返回数据。我不确定我是针对正确的类还是错误地使用 bs4 来获取我想要的信息表。

我尝试过使用不同的方法,但我找不到要从网站上抓取的信息的正确文档

>>> ```import bs4
>>> import requests
>>> res = requests.get('https://www.5dimes.eu/livelines/livelines.aspx')
>>> type(res)
<class 'requests.models.Response'>
>>> soup = bs4.BeautifulSoup(res.text)
>>> type(soup)
<class 'bs4.BeautifulSoup'>
>>> soup.title
<title>
    Live Lines
</title>
>>> soup.select('.LR Alt')
[]
>>> soup.select('.LSR')
[]
>>> soup.select('h2 > span')
[]
>>> soup = bs4.BeautifulSoup(res.text, 'lxml')
>>> type(soup)
<class 'bs4.BeautifulSoup'>
>>> soup.select('.LR Alt')
[]
>>> 
```

我想至少获得一组球队名称或赔率,但我只获得 [] 结果

【问题讨论】:

  • www.5dimes.eu - 访问被拒绝 - GDPR?

标签: python html web-scraping beautifulsoup python-requests


【解决方案1】:

这个怎么样?

from re import sub
from bs4 import BeautifulSoup
from urllib.request import urlopen
urlpage=urlopen("https://www.5dimes.eu/livelines/livelines.aspx").read()
bswebpage=BeautifulSoup(urlpage)
results=bswebpage.findAll("div",{'class':'SportSubTypes'})
for result in results:
    print(result)

结果:

<span class="Group SportSubType">
<input id="c_23" type="checkbox"/> <a href="#ac_23" rs:checkbox="c_23" title="Scroll to the latest lines for this sport.">Rugby Union Futures</a>
</span>
<span class="Group SportSubType">
<input id="c_24" type="checkbox"/> <a href="#ac_24" rs:checkbox="c_24" title="Scroll to the latest lines for this sport.">Snooker Futures</a>
</span>
</div>

【讨论】:

  • 谢谢!不是我要找的东西,但你代码中的 find.all 函数帮助我找到了答案:)
  • 请点击“这个答案很有用”。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-10
  • 2020-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多