python - 无法使用请求从网页中获取所有链接

标签 python python-3.x web-scraping beautifulsoup re

我正在尝试将所有链接连接到此 webpage 中的每个图像.

如果让 selenium 脚本向下滚动直到到达底部,我可以获得所有链接。我希望抓取的链接之一是 this one .

现在,我的目标是使用请求解析所有这些链接。我注意到我想解析的链接是使用这样的 B-uPwZsJtnB 构建的短代码。

然而,
我试图刮掉那些不同的 shortcode在该网页的页面源中找到的脚本标记中可用。周围有600 shortcodes在那个页面。我创建的脚本只能解析第一个 70shortcode最终可以建立70个合格的链接。

如何使用请求获取所有 600 个链接?

到目前为止,我已经尝试过:

import re
import json
import requests

base_link = 'https://www.instagram.com/p/{}/'
lead_url = 'https://www.instagram.com/explore/tags/baltimorepizza/'

with requests.Session() as s:
    s.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36'
    req = s.get(lead_url)
    script_tag = re.findall(r"window\._sharedData[^{]+(.*?);",req.text)[0]
    for item in json.loads(script_tag)['entry_data']['TagPage']:
        tag_items = item['graphql']['hashtag']['edge_hashtag_to_media']['edges']
        for elem in tag_items:
            profile_link = base_link.format(elem['node']['shortcode'])
            print(profile_link)

最佳答案

如果你想用 requests那么请考虑查询XHR/Ajax Http请求来模拟Lazy load。请看下图:
enter image description here
您向 instagram.com 服务器发出类似于 Scrape a JS Lazy load page by Python requests 的查询邮政。
免责声明
由于某些动态 cookie 值或 Instagram 强加的其他抓取预防措施,您可能无法成功完成该任务。

关于python - 无法使用请求从网页中获取所有链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62022907/

相关文章:

algorithm - 对超集进行分区并获取每个分区的原始集列表

python - 在 Tkinter 中集成 click-Terminal?

python - 我们可以将 XPath 与 BeautifulSoup 一起使用吗?

javascript - Nightmarejs-如何读取表行内容?

python - 如何使用给定列表的 for 循环反转 Python 中的索引值

python - 有没有办法在 Python 中定义 float 组?

python - PyCrypto - 初始化向量如何工作?

python - 如何使用 Socks5 代理抓取请求?

python - Sprite 碰撞

python - 将输出重定向到 Python 脚本中的文件