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

标签 python twitter web-scraping beautifulsoup html-parsing

这是对我的帖子 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/

相关文章:

python - 如何使用 Beautifulsoup-python 从 div 内特定标题中的段落元素中提取网页文本

php - 在 Goutte 中设置代理

python - 使用 selenium 从 textarea 清除文本

python 列表理解(如果,继续,中断)

javascript - 如何自定义默认 Twitter 小部件的 javascript 的默认 html 输出?

javascript - 如何在不影响浏览器历史记录的情况下实现导航?

python - 无法在 python 中导入 tensorfflow

python Pandas : How to group group two colums into a common column

node.js - 获取 HTTP 错误 : 400 Bad Request on GIF or video upload on Twitter

css - 在 Node.js 中进行网页抓取,以获取应用于网页每个元素的所有 CSS 属性