[英]Why am I receiving this error? IndexError: list index out of range
Disclaimer: I'm somewhat of a noob in Python so there's that.免责声明:我在 Python 方面有点菜鸟,所以就是这样。
I'm trying to webscrape this number from a website, but I keep getting this error.我试图从网站上抓取这个数字,但我一直收到这个错误。
price = soup.find_all('div', {'id':'ember105'})[0].find('text').text
IndexError: list index out of range
I'm trying to retrieve that number 14,795
我正在尝试检索那个数字14,795
What am I doing wrong?我究竟做错了什么?
Python and HTML below下面的 Python 和 HTML
import bs4
import requests
from bs4 import BeautifulSoup
r = requests.get('https://covid19.min-saude.pt/')
soup = bs4.BeautifulSoup(r.text,"lxml")
nCases= soup.find_all('div', {'id':'ember106'})[0].find('text').text
print('number cases = ' + str(nCases))
<div id="ember106" class="flex-fix allow-shrink indicator-center-text responsive-text flex-vertical ember-view"><svg class="responsive-text-group" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0.0000043483596527948976 -0.16839377582073212 439.04144287109375 160.01295471191406" width="208" height="50">
<g class="responsive-text-icon">
<!---->
</g>
<g class="responsive-text-label">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 -153 529.609375 193" height="160" width="439.0544041450777">
<text vector-effect="non-scaling-stroke" style="fill: rgb(255, 170, 0); stroke-width: 2; font-size: 160px; line-height: normal;">14,795</text>
</svg>
</g>
</svg></div>
It is because you are not finding anything.那是因为你没有找到任何东西。
In the below command...在下面的命令...
nCases= soup.find_all('div', {'id':'ember106'})[0] ...
You assume that you find something in the find_all() command.您假设您在 find_all() 命令中找到了一些东西。 It seems as though that is not a safe assumption.这似乎不是一个安全的假设。 Check that the size of the results of the find_all() function is greater than 0.检查 find_all() 函数的结果大小是否大于 0。
You may want to do the same with the find() function that is chained after it您可能希望对链接在它之后的 find() 函数执行相同操作
Looks like the data you're looking for is not in the initial page load;看起来您要查找的数据不在初始页面加载中; if you check r.text
, it doesn't contain any elements called "ember106".如果您检查r.text
,它不包含任何名为“ember106”的元素。
On the web page, it's shown in an iframe, which itself loads it via JavaScript (possibly as JSON).在网页上,它显示在 iframe 中,它本身通过 JavaScript(可能作为 JSON)加载它。
You'll either have to (a) scrape via Selenium so that all the machinery can run, or (b) trace through it by hand, figure out where the JSON data is coming from and get it there directly.您要么必须 (a) 通过 Selenium 进行抓取,以便所有机器都可以运行,要么 (b) 手动跟踪它,找出 JSON 数据的来源并直接将其发送到那里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.