首页 > 解决方案 > 登录 instagram 以抓取用户信息

问题描述

我需要从 instagram 用户页面上抓取信息,更多,我需要使用这个 url 页面: "https://www.instagram.com/cristiano/?__a=1"

问题是我需要使用我的 instagram 帐户登录才能执行此脚本

from requests import get
from bs4 import BeautifulSoup
import json
import re
import requests


url_user = "https://www.instagram.com/cristiano/?__a=1"
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.74 Safari/537.36 Edg/79.0.309.43'}

response = get(url_user, headers=headers)
print(response)

# print(page.text)
soup = BeautifulSoup(response.text, 'html.parser')

# print(soup)
jsondata=json.loads(str(soup))

我收到此错误:

JSONDecodeError:期望值:第 1 行第 1 列(字符 0)

如何避免连接问题来抓取信息和访问数据?

谢谢

标签: pythonweb-scrapingbeautifulsoupinstagram

解决方案


添加__a=1参数可以获得 JSON 响应,因此您无需通过 BeautifulSoup,您只需直接加载 JSON。

response = get(url_user, headers=headers)
jsondata=json.loads(response.text)

或者,您可以使用该json()函数来加载 JSON:

response = get(url_user, headers=headers)
jsondata = response.json()

推荐阅读