【发布时间】:2021-01-26 06:22:05
【问题描述】:
我可以使用 BeautifulSoup 从网络中提取数据,即单词列表。数据收集在组件 synonyms[i].text 中。但是,当我想将提取的数据转换为数据框时,我会将单词拆分为字母而不是完整的单词。如何将数据转换为正确数据框中的正确单词列表,即,像“分析”这样的单词在数据框中作为“分析”而不是拆分为“a”、“n”、“a”, 'l','y','s','e' ?
import requests
from bs4 import BeautifulSoup
import pandas as pd
page = requests.get("https://www.wordhippo.com/what-is/another-word-for/guard.html")
soup = BeautifulSoup(page.content, 'html.parser')
keyword = "guard"
synonyms = soup.select('.relatedwords')
for i in range(0, 1):
print ('synonyms section ' + str(i + 1))
print pd.DataFrame((list(synonyms[i].text)))
#Output that I need to convert into a DataFrame
synonyms section 1
fighter
trooper
warrior
serviceman
#The Output I am getting in the list
提前致谢。
【问题讨论】:
-
请从intro tour 重复on topic 和how to ask。 “告诉我如何解决这个编码问题?”与 Stack Overflow 无关。您必须诚实地尝试解决方案,然后就您的实施提出具体问题。
-
您还需要说明您的问题。为初学者显示输入和所需的输出。没有理由让
requests和BeautifulSoup参与您的帖子,因为它们不是您问题的一部分。只需硬编码“同义词”的示例列表,然后从那里提出您的问题。 -
@Prune。我正在使用 beautifulsoup 提取数据。但如果这样更容易,我会添加一个硬编码列表。感谢您帮助我解决问题。
-
@Prune。我已经进一步更新了我的问题。感谢您帮助我提出问题。
-
我投票支持重新打开,因为答案已被编辑/改进。
标签: python pandas list dataframe