Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.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 为什么这个漂亮的代码不能解析我的目标文本?_Python_Web Scraping_Beautifulsoup_Edgar - Fatal编程技术网

Python 为什么这个漂亮的代码不能解析我的目标文本?

Python 为什么这个漂亮的代码不能解析我的目标文本?,python,web-scraping,beautifulsoup,edgar,Python,Web Scraping,Beautifulsoup,Edgar,我正在尝试选择这个10K文件中的属性部分标题;一旦从中选择,我打算获取该部分的文本,即属性和法律程序部分标题之间的所有文本 当我运行下面的代码时,我得到了索引器“列表索引超出范围”,但我不明白为什么 文本属性似乎位于“p”标记内。我也尝试过使用'id=ITEM\u 2\u PROPERTIES'而不是text=,但这也不起作用 我哪里做错了 import requests from bs4 import BeautifulSoup url = 'https://www.sec.gov/ix?

我正在尝试选择这个10K文件中的属性部分标题;一旦从中选择,我打算获取该部分的文本,即属性和法律程序部分标题之间的所有文本

当我运行下面的代码时,我得到了索引器“列表索引超出范围”,但我不明白为什么 文本属性似乎位于“p”标记内。我也尝试过使用'id=ITEM\u 2\u PROPERTIES'而不是text=,但这也不起作用

我哪里做错了

import requests
from bs4 import BeautifulSoup


url = 'https://www.sec.gov/ix?doc=/Archives/edgar/data/1318605/000156459020004475/tsla-10k_20191231.htm'
soup = BeautifulSoup(requests.get(url).content, 'lxml')

properties_header = soup.find_all('p', text="PROPERTIES")[0]

print(properties_header)

这是因为您正在向JS呈现的站点发出请求,所以没有具有文本属性的p

但是,如果更改目标URL,则有一个:

import requests
from bs4 import BeautifulSoup


url = 'https://www.sec.gov/Archives/edgar/data/1318605/000156459020004475/tsla-10k_20191231.htm'
soup = BeautifulSoup(requests.get(url).content, 'lxml')

properties_header = soup.find_all('p', text="PROPERTIES")

print(properties_header)
输出:

[<p id="ITEM_2_PROPERTIES" style="margin-bottom:0pt;margin-top:0pt;font-weight:bold;font-style:normal;text-transform:none;font-variant: normal;font-family:Times New Roman;font-size:10pt;">PROPERTIES</p>]
我从开发者工具中获得了新的目标URL。当您重新打开JS时,会出现此问题。所以,我想你应该把这个URL作为你未来请求的目标