使用BeautifulSoup爬取Instagram粉丝数失败,函数始终返回-1求助
使用BeautifulSoup爬取Instagram粉丝数失败,函数始终返回-1求助
各位大佬好!我最近想实现批量爬取一组Instagram用户名对应的粉丝数,用的是BeautifulSoup库解析页面。但写好代码后发现,不管传入哪个有效的用户名,函数每次都返回-1,完全拿不到想要的粉丝数据,有没有人能帮我排查下问题呀?
我的代码如下:
import requests from bs4 import BeautifulSoup def instagram_followers(username): headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3' } response = requests.get(f'https://www.instagram.com/{username}/') soup = BeautifulSoup(response.text, 'html.parser') info = soup.find('meta', property='og:description') if info: followers = info['content'].split(" ")[0] return followers else: return -1
我自己简单排查了下,发现问题应该出在soup.find('meta', property='og:description')这一行——它一直找不到对应的meta标签,所以每次都走了else分支返回-1。有没有大佬知道这是为什么,或者有没有其他方法能拿到粉丝数呀?
备注:内容来源于stack exchange,提问作者antocirino
相关产品推荐
相关产品推荐

