我有一些代码,可以根据单词10的长度从单词hippo中提取单词列表。在本例中,我试图获取所有“概述”的同义词(长度为10),但发现任何包含空格的同义词都不会通过,例如,即使我指定的字母长度为10,“脑波”也不会通过。
我的这段代码列出了不包含空格的单词。我如何修改它以获得“脑波”这类有空格的同义词?
“”“
import requests
from bs4 import BeautifulSoup
import pandas as pd
page = requests.get("https://www.wordhippo.com/what-is/another-word-for/overview.html")
soup = BeautifulSoup(page.content, 'html.parser')
'Change this'
keyword = "overview"
listing = []
length = 10
synonyms = soup.select('.relatedwords')
for i in range(0, 100):
print ('synonyms section ' + str(i + 1))
csvname = str(keyword) + str(i) + ".xlsx"
print ('synonyms section ' + str(i + 1))
DataFrame = pd.DataFrame((synonyms[i].text.strip().split()))
InitList = DataFrame[0].tolist()
for item in InitList:
if len(item) == length:
listing.append(item)
print (listing)
DataFrame1 = pd.DataFrame(listing)
out_path = "FIlePathName" + csvname
DataFrame1.to_excel(out_path, index = False)
“”“
我有正确的FilePath。
提前谢谢。
发布于 2022-02-21 07:01:30
我发现我需要更改以下代码行:
DataFrame = pd.DataFrame((synonyms[i].text.strip().split()))
到这一行代码
DataFrame = pd.DataFrame((synonyms[i].text.split("\n")))
https://stackoverflow.com/questions/71200694
复制相似问题