仅当以'#'开始时,如何获取数据。 Python

发布于 2025-02-11 14:46:41 字数 632 浏览 1 评论 0原文

您好,我想从字幕中获取主题标签列表。 只有仅数据以“#”开头,

hashtags = driver.find_elements(By.CSS_SELECTOR, "li > div > div > div._a9zr > div._a9zs > span > a")

for tag in hashtags:
    print(tag.text)

​​它仍然与 @ data混合在一起,才会刮擦数据

@flutter_coding_
#softwareengineer
#iosdeveloper
#webdeveloper
#flutterdeveloper
#frontenddeveloper
#appdeveloper
#programming
#softwareengineer
#coding
#code
#100daysofcode
#javascript
#reactjs
#developer
#developerlife
#programminghumor
#coderlife
#python
#php
#desksetup
#appdevelopment
#uidesign
#frontend
#backenddeveloper
#codinggirl
#flutter

Hello im trying to get hashtags list from caption..
only will scraping data if only the data start with '#'

hashtags = driver.find_elements(By.CSS_SELECTOR, "li > div > div > div._a9zr > div._a9zs > span > a")

for tag in hashtags:
    print(tag.text)

it still mixed with @ data

@flutter_coding_
#softwareengineer
#iosdeveloper
#webdeveloper
#flutterdeveloper
#frontenddeveloper
#appdeveloper
#programming
#softwareengineer
#coding
#code
#100daysofcode
#javascript
#reactjs
#developer
#developerlife
#programminghumor
#coderlife
#python
#php
#desksetup
#appdevelopment
#uidesign
#frontend
#backenddeveloper
#codinggirl
#flutter

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

稀香 2025-02-18 14:46:42

您可以通过理解来过滤列表:

hashtags = ["@flutter_coding_", "#softwareengineer", "#iosdeveloper"]
hashtags = [i for i in hashtags if i[0]=="#"]
print(hashtags )

输出:

['#softwareengineer', '#iosdeveloper']

You cal filter the list with a comprehension:

hashtags = ["@flutter_coding_", "#softwareengineer", "#iosdeveloper"]
hashtags = [i for i in hashtags if i[0]=="#"]
print(hashtags )

Output:

['#softwareengineer', '#iosdeveloper']
明月松间行 2025-02-18 14:46:41

您可以使用startswith()方法进行排序并获取仅包含的数据项

for tag in hashtags:
    if tag.startswith('#'):
        print(tag.text)

尝试:
首先,您必须调用.text方法,然后startswith()`与条件

for tag in hashtags:
    tag=tag.text
    if tag.startswith('#'):
        print(tag)

You can use startswith() method to sort and to get the data items those contain only #

for tag in hashtags:
    if tag.startswith('#'):
        print(tag.text)

Try:
At first,you have to invoke .text method then startswith()` method with if condition

for tag in hashtags:
    tag=tag.text
    if tag.startswith('#'):
        print(tag)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文