Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么我的网页抓取会生成HTML,但却赢得了';不返回任何文本?_Python_Web Scraping_Data Science - Fatal编程技术网

Python 为什么我的网页抓取会生成HTML,但却赢得了';不返回任何文本?

Python 为什么我的网页抓取会生成HTML,但却赢得了';不返回任何文本?,python,web-scraping,data-science,Python,Web Scraping,Data Science,这里是新的编码员。我正在尝试从以下网站返回所有每股收益数据: 我一开始很慢,只是尝试返回“March”,并使用以下代码: from bs4 import BeautifulSoup from requests import get url = "https://www.nasdaq.com/market-activity/stocks/csco/revenue-eps" response = get(url) soup = BeautifulSoup(response.text, 'html.

这里是新的编码员。我正在尝试从以下网站返回所有每股收益数据:

我一开始很慢,只是尝试返回“March”,并使用以下代码:

from bs4 import BeautifulSoup
from requests import get

url = "https://www.nasdaq.com/market-activity/stocks/csco/revenue-eps"
response = get(url)
soup = BeautifulSoup(response.text, 'html.parser')

month = soup.find("th", {"class": "revenue-eps__cell revenue-eps__cell--rowheading"})

print(month.text)
当我运行它时,没有错误,但没有返回任何内容。
当我尝试运行相同的代码,但改用
print(month)
时,我会从如下元素返回HTML:
th class=“revenue-eps\uu单元格revenue-eps\uu单元格--行标题”scope=“row”>/th>


我注意到在返回的HTML中,文本不在
th
中。为什么呢?是我做错了什么,还是我正在努力清理的网站?

数据不是嵌入在页面中,而是从API检索的。您可以将公司名称作为参数传递,直接获取所有数据:

import requests
import json

company = "CSCO"
r = requests.get("https://api.nasdaq.com/api/company/{}/revenue?limit=1".format(company))

print(json.loads(r.text)['data'])

如果您在禁用JS的情况下访问该页面,则该表不会填充,因此我猜您的解析器就是这样看待它的。我的下一个建议是尝试查看进行了哪些Ajax调用,或者加载了哪些脚本来加载您正在查找的内容-您可能需要直接调用这些脚本来获取数据,而不是查看生成的HTML。请注意,limit=1给出了2020201198 limit=2 201720162015等等。它成功了,谢谢!你知道在哪里可以找到这个api的文档吗?我正在尝试使用它,以便更好地组织数据…@Rosslavitt我不认为你会找到文档,除非这些api有付费服务。我记得我回答了这个问题,它也将这个api用于其他目的。如果你能接受这个答案,请随意接受