从Facebook中提取带有美丽汤的用户名



我想在没有API的情况下从Facebook帖子中提取用户名。我已经成功提取了时间戳,但相同的算法不适用于用户名。
作为输入,我有一个这样的链接列表:

https://www.facebook.com/barackobama/photos/a.10155401589571749/10156901908101749/?type=3&theater

https://www.facebook.com/photo.php?fbid=391679854902607&set=gm.325851774772841&type=1&theater

https://www.facebook.com/FisherHouse/photos/pcb.10157433176029134/10157433170239134/?type=3&theater

我已经尝试使用pageTitle进行搜索,但是由于有许多无用的信息,因此无法按预期工作。
facebook = BeautifulSoup(req.text, "html.parser") facebookusername = str (facebook.select('[id="pageTitle"]'))

我现在的代码是:

req = requests.get(url)
facebook = BeautifulSoup(req.text, "html.parser")
divs = facebook.find_all('div', class_="_title")
for iteration in range (len(divs)):
if 'title' in str(divs[iteration]):
print (divs[iteration])

我只需要用户名作为输出。

正如WizKid所说,你应该使用API。但给你一个答案:页面的名称似乎嵌套在 h5 标题中。首先提取 h5,然后获取名称。

x = facebook.find('h5')
title = x.find('a').getText()

我目前不能尝试,但这应该可以解决问题。

最新更新