Python 3.x (Python)-如何在结构化Python列表中存储使用BeautifulSoup从HTML表提取的文本
我使用beautifulsoup解析网页:Python 3.x (Python)-如何在结构化Python列表中存储使用BeautifulSoup从HTML表提取的文本,python-3.x,beautifulsoup,Python 3.x,Beautifulsoup,我使用beautifulsoup解析网页: import requests from bs4 import BeautifulSoup page = requests.get("webpage url") soup = BeautifulSoup(page.content, 'html.parser') 我找到表格并打印文本 Ear_yield= soup.find(text="Earnings Yield").parent print(Ear_yield.parent.text) 然后我
import requests
from bs4 import BeautifulSoup
page = requests.get("webpage url")
soup = BeautifulSoup(page.content, 'html.parser')
我找到表格并打印文本
Ear_yield= soup.find(text="Earnings Yield").parent
print(Ear_yield.parent.text)
然后我得到表中一行的输出
Earnings Yield
0.01
-0.59
-0.33
-1.23
-0.11
我希望将此输出存储在列表中,以便在xls上打印并对元素进行操作(例如if(收益率[0]>收益率[1])。
所以我写:
import html2text
text1 = Ear_yield.parent.text
Ear_yield_text = html2text.html2text(pr1)
list_Ear_yield = []
for i in Ear_yield_text :
list_Ear_yield.append(i)
认为我的web数据已进入列表。我打印第四项并检查:
print(list_Ear_yield[3])
我期望输出为-0.33,但我得到
n
这意味着列表中包含单个字符,而不是完整的单词:
请让我知道我哪里做错了,这是因为您的
Ear\u yield\u text
是字符串而不是列表。假设文本有新行,您可以直接执行以下操作:
list_Ear_yield = Ear_yield_text.split('\n')
现在,如果你打印列表,你会得到这个结果
['Earnings Yield', '0.01', '-0.59', '-0.33', '-1.23', '-0.11']