【发布时间】:2018-04-27 08:19:43
【问题描述】:
长话短说,我正在尝试创建一个 Instagram python 抓取工具,它可以加载整个页面并抓取图片的所有链接。我让它工作了,唯一的问题是,它只加载 Instagram 显示的原始 12 张照片。无论如何我可以告诉请求加载整个页面吗?
工作代码;
import json
import requests
from bs4 import BeautifulSoup
import sys
r = requests.get('https://www.instagram.com/accountName/')
soup = BeautifulSoup(r.text, 'lxml')
script = soup.find('script', text=lambda t: t.startswith('window._sharedData'))
page_json = script.text.split(' = ', 1)[1].rstrip(';')
data = json.loads(page_json)
non_bmp_map = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd)
for post in data['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['edges']:
image_src = post['node']['display_url']
print(image_src)
【问题讨论】:
-
BS4 是错误的工具。由于像 Instagram 这样的页面具有“无限滚动”功能,当页面滚动到底部时会显示额外的内容,你需要一个像 selenium 这样的刮板,它会调用浏览器来加载并进行滚动。尝试启动here
标签: python python-3.x python-requests instagram