python - 如何将我的结果变成字符串列表?
问题描述
我想将我的结果(包括顶级高音扬声器趋势)列在一个列表中。稍后我将使用这个列表项在谷歌新闻中用作查询。谁能告诉我如何将我的结果作为一个列表,其次我将如何使用列表项作为谷歌新闻中的单独查询(我只需要如何做到这一点。我已经有一个代码)
这是我的代码:
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
先感谢您。
解决方案
要制作新列表,请执行
newlist = []
for result in top_trends[0:10]:
newlist.append(result.text)
或通过列表理解
newlist = [result.text for result in top_trends[0:10]]
推荐阅读
- c# - 使用 CommandLineParser 指定相同类型的多个输入
- java - 检查与父页面融合的子页面可用性的最佳方法?
- angular - 在之间添加对象
- 在
- 在 Angular 项目中
- 在
- python - cx_freeze 错误:找不到模块 tkinter
- javascript - Javascript:以 0 作为输入时,toFixed 函数返回 NaN
- python - 使用逻辑索引列表访问 numpy 数组会打印该列表
- android - 如何将数据从一个非活动类发送到另一个 android
- css - 选项列表在我的模态中制作滚动条,而不是位于模态的顶部
- spring-cloud-dataflow - Spring Cloud Dataflow - 组合任务运行器不启动第二个任务
- mysql - 合并两个查询并添加一个新列