无法解析网页中的其他产品链接 - python

我已经在Python中创建了一个脚本来从网页中获取不同的产品链接。尽管我知道该站点的内容是动态的,但是我尝试了传统方式让您知道我尝试过的内容。我在开发工具中寻找API,但找不到一个。没有使用请求获取那些链接的方法吗?

Site Link

到目前为止,我已经写过:

import requests
from bs4 import BeautifulSoup

link = "https://www.amazon.com/stores/node/10699640011"

def fetch_product_links(url):
    res = requests.get(url,headers={"User-Agent":"Mozilla/5.0"})
    soup = BeautifulSoup(res.text,"lxml")
    for item_link in soup.select("[id^='ProductGrid-'] li[class^='style__itemOuter__'] > a"):
        print(item_link.get("href"))

if __name__ == '__main__':
    fetch_product_links(link)

如何使用请求从该站点获取不同的产品链接?

参考方案

我认为您只需要从网络标签中可以看到的其他网址结构中收集的asins,即可大大缩短最终网址。但是,您确实需要向原始网址提出请求,以选择要在第二个网址中使用的标识符。返回146个链接。

import requests, re, json

node = '10699640011'

with requests.Session() as s:
    r = s.get(f'https://www.amazon.com/stores/node/{node}')
    p = re.compile(r'var slotsStr = "\[(.*?,){3} share\]";')
    identifier = p.findall(r.text)[0]
    identifier = identifier.strip()[:-1]
    r = s.get(f'https://www.amazon.com/stores/slot/{identifier}?node={node}')
    p = re.compile(r'var config = (.*?);')
    data = json.loads(p.findall(r.text)[0])
    asins = data['content']['ASINList']
    links = [f'https://www.amazon.com/dp/{asin}' for asin in asins]
    print(links)

编辑:

有两个给定的节点:

import requests, re, json
from bs4 import BeautifulSoup as bs

nodes = ['3039806011','10699640011']

with requests.Session() as s:
    for node in nodes:
        r = s.get(f'https://www.amazon.com/stores/node/{node}')
        soup = bs(r.content, 'lxml')
        identifier = soup.select('.stores-widget-btf:not([id=share],[id*=RECOMMENDATION])')[-1]['id']
        r = s.get(f'https://www.amazon.com/stores/slot/{identifier}?node={node}')
        p = re.compile(r'var config = (.*?);')
        data = json.loads(p.findall(r.text)[0])
        asins = data['content']['ASINList']
        links = [f'https://www.amazon.com/dp/{asin}' for asin in asins]
        print(links)

Python-使用请求时发布请求失败 - python

使用外壳程序时,我可以通过运行以下命令成功创建新用户curl --user administrator:pasword "Content-Type: application/json" https://localhost:8080/midpoint/ws/rest/users -d @user.json但是,当我尝试使用请求在python…

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

Python exchangelib在子文件夹中读取邮件 - python

我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

Python ThreadPoolExecutor抑制异常 - python

from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED def div_zero(x): print('In div_zero') return x / 0 with ThreadPoolExecutor(max_workers=4) as execut…