问题
I need to gather some information which is not provided by Facebook Analytics. For example, the original url and headline of an article promoted on Facebook as a link post. This info is buried in the html code of a Facebook post but I struggle to dig it out. Will appreciate your help.
Let's take this example: https://www.facebook.com/bbcnews/posts/10156428513547217
I identified classes for a link (bbc.in...): "_6ks" and headline: 'mbs _6m6 _2cnj _5s6c'
The code below doesn't return anything:
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'))
回答1:
Another way to achieve the same would be something like below:
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)
回答2:
The reason you are not able to getting any output is b'coz both of those divs are placed cleverly placed within comment tags <!--
-->
. Comments are ignored by the parsers. If you print the soup, both of the divs are present but within the comment tags.
We can get the comments and then make a new soup using that to bypass this.
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)
Output
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
来源:https://stackoverflow.com/questions/54309624/scrape-original-links-and-headlines-from-facebook-posts