首页 > 解决方案 > 如何将我的结果变成字符串列表?

问题描述

我想将我的结果(包括顶级高音扬声器趋势)列在一个列表中。稍后我将使用这个列表项在谷歌新闻中用作查询。谁能告诉我如何将我的结果作为一个列表,其次我将如何使用列表项作为谷歌新闻中的单独查询(我只需要如何做到这一点。我已经有一个代码)

这是我的代码:

url = "https://trends24.in/pakistan"
req = requests.get(url)
re = req.content
soup = BeautifulSoup(re, "html.parser")
top_trends = soup.findAll("li", class_ = "")
top_trends1 = soup.find("a", {"target" : "tw"})
for result in top_trends[0:10]:
    print(result.text)

输出是:

#JusticeForUsamaNadeemSatti25K
#IslamabadPolice10K
#promotemedicalstudents51K
#ArrestSheikhRasheed
#MWLHighlights202014K
Sahiwal
Deport Infidel Yasser Al-Habib
BOSS LADY RUBINA929K
Sheikh Nimr
G-10 Srinagar Highway

先感谢您。

标签: pythonlist

解决方案


要制作新列表,请执行

newlist = []
for result in top_trends[0:10]:
    newlist.append(result.text)

或通过列表理解

newlist = [result.text for result in top_trends[0:10]]

推荐阅读