我需要收集一些Facebook Analytics未提供的信息。例如,文章的原始网址和标题在Facebook上被宣传为链接文章。此信息被埋在Facebook帖子的html代码中,但我很难将其挖掘出来。感谢您的帮助。
让我们举个例子:https://www.facebook.com/bbcnews/posts/10156428513547217
我确定了链接的类(bbc.in ...):“ _ 6ks” 和标题:“ mbs _6m6 _2cnj _5s6c”
下面的代码不返回任何内容:
from bs4 import BeautifulSoup
import requests
link = 'https://www.facebook.com/bbcnews/posts/10156428513547217'
r = requests.get(link)
soup = BeautifulSoup(r.content, "lxml")
for paragraph in soup.find_all("div", class_="_6ks"):
for a in paragraph("a"):
print(a.get('href'))
for paragraph in soup.find_all("div", class_='mbs _6m6 _2cnj _5s6c'):
for a in paragraph("a"):
print(a.get('hover'))
答案 0 :(得分:1)
之所以无法获得任何输出,是因为这两个div都被巧妙地放置在注释标签<!--
-->
中。注释将被解析器忽略。如果您打印汤,则两个div都存在,但在注释标签中。
我们可以获取评论,然后使用它来绕汤。
from bs4 import BeautifulSoup
from bs4 import Comment
import requests
link = 'https://www.facebook.com/bbcnews/posts/10156428513547217'
headers={'User-Agent':'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0'}
r = requests.get(link,headers=headers)
soup = BeautifulSoup(r.content, "lxml")
comments=soup.find_all(string=lambda text:isinstance(text,Comment))
soup=BeautifulSoup(comments[0], "lxml")
for paragraph in soup.find_all("div", class_="_6ks"):
for a in paragraph("a"):
print(a.get('href'))
print('-------------------------------------------------------------------')
for paragraph in soup.find_all("div", class_='mbs _6m6 _2cnj _5s6c'):
for a in paragraph("a"):
print(a.text)
输出
https://l.facebook.com/l.php?u=https%3A%2F%2Fbbc.in%2F2FP4EgR&h=AT3jWrl9cgJEY-8NBLgbvOEtDSZ8dBABo4TJaVJ66QBbWdCsBypvAkN6MD7VhJoOgy_LGJeomQAlcwtex_Ab-7TvWXhKkLB1m_TjzxOSk3R2uP8qTUL3aTTj4Pcz2ZSZunWxZsPtOlJSpay_AtQfNTuLTUQ80OrtvRiDMs8duN3b27IH2UPnGThQ_YGJAcYJdPE3R9JbyxSQNhJ8yTmaRJe8pMNbgVkentXU4p3liys2IQvphwRd0V8ANmo-4xvKj1dRADHy3hOyUkcv_L2u8Z4WpLx1AZQCTitvfSLvhQRMZ0cK1vIjkuv3gfurRf250p3D54GxQZIsVLymDzNtLbOnigIuFRHfQFAUSBDzJGTqQB3hs4lilYyFXIqaC2cdXwDp8GDrmYbgRWmEMmN6A5fHDdRlF4m7MXJO0vJ_7uqkh0TAdcvTSc0dqt5Wv3wOoEN5S1b2ddLZOp3DFwApAGkSHsOtW7Pjc-STFljuV045ERsUWUbmnALSl9vxB6tiZ0poa3aGxZqnlFqsaTB-A8plwCWp5ed9JALlurBco447aELbpuRexqoOajxTvS_yW9BdSXaufzpbPFKaNt5go7uf4GjdekpITCApJo2JoAOzzsfKHdg1MXasOCw
-------------------------------------------------------------------
MPs put forward rival Brexit plans
答案 1 :(得分:1)
另一种实现此目的的方法如下:
from bs4 import BeautifulSoup
import requests
link = 'https://www.facebook.com/bbcnews/posts/10156428513547217'
res = requests.get(link,headers={'User-Agent':'Mozilla/5.0'})
comment = res.text.replace("-->", "").replace("<!--", "")
soup = BeautifulSoup(comment, "lxml")
items = soup.select_one('.mbs a')
print(items.get("href")+"\n",items.text)