【问题标题】:BeautifulSoup - not reading all links contained in hidden element in soupBeautifulSoup - 不读取汤中隐藏元素中包含的所有链接
【发布时间】:2014-04-30 11:07:43
【问题描述】:

我已经尝试了与 BeautifulSoup 相关的所有答案,但没有找到页面上的所有链接,但它们似乎都不起作用。我正在 Facebook 上进行一些学术研究,并试图从一些状态链接中抓取 /hashtag/ 元素,这些元素无法通过 FB 图形 API 获得。这是一个示例帖子:https://www.facebook.com/339278974073/posts/10151731033014074

如果我运行以下代码块:

import urllib2
from BeautifulSoup import BeautifulSoup
url = 'https://www.facebook.com/339278974073/posts/10151731033014074'
request = urllib2.Request(url)
response = urllib2.urlopen(request)
soup = BeautifulSoup(response)

然后查看变量“soup”的输出,我可以看到其中有带有“/hashtag/”的链接。大约一年前,我所要做的就是找到所有标签实例:

hashtag = soup.findAll('a', href=re.compile('/hashtag/?')) 

现在,它似乎被破坏了,因为 BeautifulSoup 没有读取包含主题标签的文本块——它们都在我可以在汤中看到的“hidden_​​elem”代码类中,但 BS 没有读取它。任何答案将不胜感激!

这是汤中 BS 没有找到任何东西的部分(我为混乱道歉):

[<code class="hidden_elem" id="u_0_c"><!--<!-- <div class="_5pcb"><div class="_5jmm  
 _5pat _5uch _5uun" data-ft="&#123;&quot;fbfeed_location&quot;:5&#125;" id="u_0_3"><div
class="clearfix userContentWrapper _5pcr"><a class="_5pb8" 
href="https://www.facebook.com/IndianaOrganProcurementOrganization" data-
ft="&#123;&quot;tn&quot;:&quot;\\u003C&quot;&#125;"><img class="_s0 _5xib _rw img" 
src="https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn2/t1.0-
1/p50x50/602346_10151254741684074_596547152_s.jpg" alt=""   /></a>
<div class="_5pax"><h5 class="_5yig _5pbw" data-
ft="&#123;&quot;tn&quot;:&quot;C&quot;&#125;"><div class="fwn fcg">
<span class="fwb fcg" data-ft="&#123;&quot;tn&quot;:&quot;k&quot;&#125;">
<a href="https://www.facebook.com/IndianaOrganProcurementOrganization">Indiana 
Organ Procurement Organization</a></span></div></h5><div class="mbs _5pbx userContent"
 data-ft="&#123;&quot;tn&quot;:&quot;K&quot;&#125;"><p>Stop by our tent and get 
your &#064;jimmybuffet <a class="_58cn"
href="https://www.facebook.com/hashtag/pencilthinmustache?source=feed_text" 
data-ft="&#123;&quot;tn&quot;:&quot;*N&quot;,&quot;type&quot;:104&#125;">
<span class="_58cl">‪#‎</span><span class="_58cm">PencilThinMustache‬</span></a>
 and <a class="_58cn" href="https://www.facebook.com/hashtag/sayyes?source=feed_text" 
 .......
[some code deleted]
<div id="substream_pagelet" data-referrer="substream_pagelet"></div> -->--></code>]

我想得到的是 /hashtag/ url 中的文本,例如“PencilThinMustache”,但我很乐意此时只获取 url。

【问题讨论】:

  • 请务必使用BeautifulSoup 4; 3 已经过时了。
  • 谢谢,我也用 BeautifulSoup 4 试过了。它仍然没有读取它。它不会在此代码块中找到任何 URL。
  • 这不是一个解决方案,只是一个一般性的评论,你不应该再使用 BS3;我从我发布链接答案的问题中识别出您的页面加载代码。
  • 再次感谢。会的。

标签: python beautifulsoup


【解决方案1】:

您的 &lt;code class="hidden_elem"&gt; 标记包含 HTML 注释,not 元素。

将它们分别解析为 HTML:

>>> comment = soup.find('code').contents[0]
>>> type(comment)
<class 'BeautifulSoup.Comment'>
>>> BeautifulSoup(comment).findAll('a', href=re.compile('/hashtag/?'))
[<a class="_58cn" href="https://www.facebook.com/hashtag/pencilthinmustache?source=feed_text" data-ft='{"tn":"*N","type":104}'><span class="_58cl">‪#‎</span><span class="_58cm">PencilThinMustache‬</span></a>, <a class="_58cn" href="https://www.facebook.com/hashtag/sayyes?source=feed_text" data-ft='{"tn":"*N","type":104}'><span class="_58cl">‪#‎</span><span class="_58cm">sayyes‬</span></a>, <a class="_58cn" href="https://www.facebook.com/hashtag/donatelife?source=feed_text" data-ft='{"tn":"*N","type":104}'><span class="_58cl">‪#‎</span><span class="_58cm">donatelife‬</span></a>]
>>> for link in BeautifulSoup(comment).findAll('a', href=re.compile('/hashtag/?')):
...     print link.text
... 
‪#‎PencilThinMustache‬
‪#‎sayyes‬
‪#‎donatelife‬

【讨论】:

  • 太棒了。谢谢!以后我也肯定现在只用bs4。
【解决方案2】:

问题是在href 属性内带有hashtag/ 的链接在html 注释内。

一种选择是查找页面上的所有 cmets 并搜索其中的链接:

comments = soup.find_all(text=lambda text:isinstance(text, Comment))
for comment in comments:
    comment_soup = BeautifulSoup(comment)
    links = comment_soup.find_all('a', href=re.compile('/hashtag/?'))
    if links:
        print links

打印:

[<a class="_58cn" data-ft='{"tn":"*N","type":104}' href="https://www.facebook.com/hashtag/pencilthinmustache?source=feed_text"><span class="_58cl">‪#‎</span><span class="_58cm">PencilThinMustache‬</span></a>, <a class="_58cn" data-ft='{"tn":"*N","type":104}' href="https://www.facebook.com/hashtag/sayyes?source=feed_text"><span class="_58cl">‪#‎</span><span class="_58cm">sayyes‬</span></a>, <a class="_58cn" data-ft='{"tn":"*N","type":104}' href="https://www.facebook.com/hashtag/donatelife?source=feed_text"><span class="_58cl">‪#‎</span><span class="_58cm">donatelife‬</span></a>]

希望对您有所帮助。

【讨论】:

  • 这也行得通!我今天学到了一些东西。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2020-10-16
  • 2016-04-05
  • 2019-05-15
  • 2018-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多