python - 登录 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)
如何避免连接问题来抓取信息和访问数据?
谢谢
解决方案
添加__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()
推荐阅读
- plugins - Intellij 没有下载插件
- python - 如何绘制对象的特定或多个轮廓
- mysql - 如何以 root 身份运行 brew?
- python - 如何在读取 excel 文件时直接解析数据框中的日期,而不是使用 parse_dates 参数?
- c# - 使用 for 循环检查字符串定义的数组元素
- java - 解析传入的短信 Android (API 26)
- c# - 发布后检索具有多个列表对象的 ViewModel
- mysql - MySQL优化几何查询
- shell - 当我在 docker 容器中执行 shell 脚本时,错误是“没有这样的文件或目录”
- gridview - 如何在 yii2 中将复选框列添加到现有的 gridview 列?