专注于特定的结果,同时用Python和BeautifulSoup4抓取Twitter?

专注于特定的结果,同时用Python和BeautifulSoup4抓取Twitter?,python,twitter,web-scraping,beautifulsoup,html-parsing,Python,Twitter,Web Scraping,Beautifulsoup,Html Parsing,这是我文章的后续内容 我没有使用twitterapi,因为它不会查看用户的tweets 这么早以前的标签。完整的代码和输出如下示例所示 我想从每条tweet中获取特定的数据name和handle正在准确地检索我要查找的内容,但我在缩小其余元素的范围方面遇到了困难 例如: link = soup('a', {'class': 'tweet-timestamp js-permalink js-nav js-tooltip'}) url = link[0] 检索以下内容: <a clas

这是我文章的后续内容

我没有使用twitterapi,因为它不会查看用户的tweets 这么早以前的标签。完整的代码和输出如下示例所示

我想从每条tweet中获取特定的数据
name
handle
正在准确地检索我要查找的内容,但我在缩小其余元素的范围方面遇到了困难

例如:

 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>
完整输出:

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>
迈克尔·皮尔 @米克佩尔乔罗

案件结案:警察局长宣布在本周退休前解决——尽管有关案件的问题越来越多

转发 4. 喜欢 2. 有人建议,在那里可能有这个问题的答案。然而,我认为这个问题及其答案没有足够的背景或解释,在更复杂的情况下没有帮助。指向Beautiful Soup文档相关部分的链接非常有用,

使用类似字典的方式访问
标记的属性

例如,要获取
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
是一个特殊的多值属性,您可以只使用其中一个类(如果这足以缩小对所需元素的搜索范围)。例如,而不是:

soup('a', {'class': 'tweet-timestamp js-permalink js-nav js-tooltip'})
您可以使用:

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


Alecxe已经解释了如何使用“href”键获取值

所以我要回答你们问题的另一部分:

类似地,retweets和favorites命令返回大块的 html,而我真正需要的是显示的数值 每一个

.contents返回所有子项的列表。由于您找到的“按钮”有几个您感兴趣的子项,因此您可以从以下解析内容列表中获取它们:

retweetcount = retweets[0].contents[3].contents[1].contents[1].string
这将返回值
4

如果您想要更具可读性的方法,请尝试以下方法:

retweetcount = retweets[0].find_all('span', class_='ProfileTweet-actionCountForPresentation')[0].string

favcount = favorites[0].find_all('span', { 'class' : 'ProfileTweet-actionCountForPresentation')[0].string
这将分别返回
4
2
。 这是因为我们转换soup/find_all返回的结果集并获取tag元素(使用[0]),然后使用find_all()再次递归地在其所有子体中查找


现在,您可以在每条推文中循环并相当轻松地提取这些信息

@KevinGuan的可能复制他的问题不仅仅是提取
href
属性。从一个增强的结果集中寻找后代有点混乱,我已经回答了。这是两个有多种解决方案的伟大答案。我意识到不可能接受两个答案都是正确的,但我希望它是。。。非常感谢。选择这个答案是因为循环示例、便于进一步搜索的词汇表以及选择多值属性的有用信息。但是,这两个都非常好。
retweetcount = retweets[0].contents[3].contents[1].contents[1].string
retweetcount = retweets[0].find_all('span', class_='ProfileTweet-actionCountForPresentation')[0].string

favcount = favorites[0].find_all('span', { 'class' : 'ProfileTweet-actionCountForPresentation')[0].string