繁体   English   中英

Python 请求不正确 URL

[英]Python Requests Not Pulling Correct URL

我正在尝试编写一个脚本,该脚本将转到https://etfdb.com/etfs/asset-class/equity/#etfs&sort_name=assets_under_management&sort_order=desc&page=1并提取网站上列出的所有 ETF。 如果您访问该页面,您将看到有 82 个页面包含信息。 您可以使用 URL 参数访问任何页面,如果您输入的页面不存在,则默认为第 1 页。每当我在请求调用中更改 URL 时,我都会继续获得与第 1 页相同的信息,而当我在浏览器中输入相同的 URL 我可以看到包含新信息的新页面。 例如, requests.get("https://etfdb.com/etfs/asset-class/equity/#etfs&sort_name=assets_under_management&sort_order=desc&page=61")返回相同的 HTML 就像我输入page=1一样,但是,当我copy and paste the URL into my browser it shows me page 61. When I inspect the request object it has reponse 200, false for redirect and the URL field is the correct one I want to pull, but the information still matches the first page. 我查看了 html 并没有看到令牌或身份验证变量(尽管它可能被混淆了所以我看不到)。 我想知道是否有人知道为什么请求不断拉第一页并忽略 URL 参数?

好的,所以实际数据来自 API 在第 1 页试试这个:

import requests


headers = {
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0',
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'Accept-Language': 'en-US,en;q=0.5',
    'Content-Type': 'application/json',
    'X-Requested-With': 'XMLHttpRequest',
    'DNT': '1',
    'Connection': 'keep-alive',
    'Referer': 'https://etfdb.com/etfs/asset-class/equity/',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-origin',
}

response = requests.get('https://etfdb.com/data_set/?tm=92718&cond={%22by_type%22:[%22Etfdb::EtfType%22,17,null,false,false]}&no_null_sort=true&count_by_id=&limit=25&sort=assets_under_management&order=desc&limit=25&offset=25', headers=headers)

然后对于第二页,将&offset=25中的偏移量增加到 50。 如果您想刮掉所有这些,您可以更改“限制”参数或除以前 25 次的 json 响应中返回的“总计”并迭代 ~total/25 次。 我把它留给你

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM