gpt4 book ai didi

python - 在使用 Python 和 Beautiful Soup 4 抓取 Twitter 时关注特定结果?

转载 作者:太空宇宙 更新时间:2023-11-03 14:14:00 24 4
gpt4 key购买 nike

这是对我的帖子 Using Python to Scrape Nested Divs and Spans in Twitter? 的跟进.

我没有使用 Twitter API,因为它不查看推文这么远的标签。示例后的完整代码和输出如下。

我想从每条推文中抓取特定数据。 namehandle 正在检索我正在寻找的内容,但我无法缩小其余元素的范围。

举个例子:

 link = soup('a', {'class': 'tweet-timestamp js-permalink js-nav js-tooltip'})
url = link[0]

检索这个:

 <a class="tweet-timestamp js-permalink js-nav js-tooltip" href="/Mikepeeljourno/status/648787700980408320" title="2:13 AM - 29 Sep 2015">
<span class="_timestamp js-short-timestamp " data-aria-label-part="last" data-long-form="true" data-time="1443518016" data-time-ms="1443518016000">29 Sep 2015</span></a>

对于 url,我只需要第一行的 href 值。

同样,retweetsfavorites 命令返回大块 html,而我真正需要的只是为每个显示的数值。

如何将结果缩小到 url、转发计数和收藏计数输出所需的数据?

我计划在我开始工作后通过所有推文循环,以防对您的建议产生影响。

完整代码:

 from bs4 import BeautifulSoup
import requests
import sys

url = 'https://twitter.com/search?q=%23bangkokbombing%20since%3A2015-08-10%20until%3A2015-09-30&src=typd&lang=en'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'}
r = requests.get(url, headers=headers)
data = r.text.encode('utf-8')
soup = BeautifulSoup(data, "html.parser")

name = soup('strong', {'class': 'fullname js-action-profile-name show-popup-with-id'})
username = name[0].contents[0]

handle = soup('span', {'class': 'username js-action-profile-name'})
userhandle = handle[0].contents[1].contents[0]

link = soup('a', {'class': 'tweet-timestamp js-permalink js-nav js-tooltip'})
url = link[0]

messagetext = soup('p', {'class': 'TweetTextSize js-tweet-text tweet-text'})
message = messagetext[0]

retweets = soup('button', {'class': 'ProfileTweet-actionButtonUndo js-actionButton js-actionRetweet'})
retweetcount = retweets[0]

favorites = soup('button', {'class': 'ProfileTweet-actionButtonUndo u-linkClean js-actionButton js-actionFavorite'})
favcount = favorites[0]

print (username, "\n", "@", userhandle, "\n", "\n", url, "\n", "\n", message, "\n", "\n", retweetcount, "\n", "\n", favcount) #extra linebreaks for ease of reading

完整输出:

Michael Peel

@Mikepeeljourno

<a class="tweet-timestamp js-permalink js-nav js-tooltip" href="/Mikepeeljourno/status/648787700980408320" title="2:13 AM - 29 Sep 2015"><span class="_timestamp js-short-timestamp " data-aria-label-part="last" data-long-form="true" data-time="1443518016" data-time-ms="1443518016000">29 Sep 2015</span></a>

<p class="TweetTextSize js-tweet-text tweet-text" data-aria-label-part="0" lang="en"><a class="twitter-hashtag pretty-link js-nav" data-query-source="hashtag_click" dir="ltr" href="/hashtag/FT?src=hash"><s>#</s><b>FT</b></a> Case closed: <a class="twitter-hashtag pretty-link js-nav" data-query-source="hashtag_click" dir="ltr" href="/hashtag/Thailand?src=hash"><s>#</s><b>Thailand</b></a> police chief proclaims <a class="twitter-hashtag pretty-link js-nav" data-query-source="hashtag_click" dir="ltr" href="/hashtag/Bangkokbombing?src=hash"><s>#</s><b><strong>Bangkokbombing</strong></b></a> solved ahead of his retirement this week -even as questions over case grow</p>

<button class="ProfileTweet-actionButtonUndo js-actionButton js-actionRetweet" data-modal="ProfileTweet-retweet" type="button">
<div class="IconContainer js-tooltip" title="Undo retweet">
<span class="Icon Icon--retweet"></span>
<span class="u-hiddenVisually">Retweeted</span>
</div>
<div class="IconTextContainer">
<span class="ProfileTweet-actionCount">
<span aria-hidden="true" class="ProfileTweet-actionCountForPresentation">4</span>
</span>
</div>
</button>

<button class="ProfileTweet-actionButtonUndo u-linkClean js-actionButton js-actionFavorite" type="button">
<div class="IconContainer js-tooltip" title="Undo like">
<div class="HeartAnimationContainer">
<div class="HeartAnimation"></div>
</div>
<span class="u-hiddenVisually">Liked</span>
</div>
<div class="IconTextContainer">
<span class="ProfileTweet-actionCount">
<span aria-hidden="true" class="ProfileTweet-actionCountForPresentation">2</span>
</span>
</div>
</button>

建议BeautifulSoup - extracting attribute values那里可能有这个问题的答案。但是,我认为这个问题及其答案没有足够的上下文或解释来帮助解决更复杂的情况。 Beautiful Soup 文档相关部分的链接很有帮助,http://www.crummy.com/software/BeautifulSoup/documentation.html#The%20attributes%20of%20Tags

最佳答案

使用类似字典的访问 标签 的属性。

例如获取href属性值:

links = soup('a', {'class': 'tweet-timestamp js-permalink js-nav js-tooltip'})
url = link[0]["href"]

或者,如果您需要获取找到的每个链接的 href 值:

links = soup('a', {'class': 'tweet-timestamp js-permalink js-nav js-tooltip'})
urls = [link["href"] for link in links]

作为旁注,您不需要指定完整的 class 值来定位元素。 class 是一个特殊的多值属性,您可以只使用其中一个类(如果这足以缩小所需元素的搜索范围)。例如,而不是:

soup('a', {'class': 'tweet-timestamp js-permalink js-nav js-tooltip'})

您可以使用:

soup('a', {'class': 'tweet-timestamp'})

或者,一个 CSS selector :

soup.select("a.tweet-timestamp")

关于python - 在使用 Python 和 Beautiful Soup 4 抓取 Twitter 时关注特定结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35006682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com