requests_html中文文档,requestshtml找不到页面元素

在使用requests模块和BeautifulSoup登录之后,可以使用我在注释中建议的链接来解析json中可用的所需数据。下面的脚本应该得到您的名称,数量,价格和相关产品的链接。你只能得到21个产品使用下面的脚本。在这个json内容中有一个分页选项。您可以通过使用分页来获得所有产品。在import json

import requests

from bs4 import BeautifulSoup

baseurl = 'https://www.instacart.com/store/'

data_url = "https://www.instacart.com/v3/retailers/159/module_data/dynamic_item_lists/cart_starters/storefront_canonical?origin_source_type=store_root_department&tracking.page_view_id=b974d56d-eaa4-4ce2-9474-ada4723fc7dc&source=web&cache_key=df535d-6863-f-1cd&per=30"

data = {"user": {"email": "alexanderjbusch@gmail.com", "password": "password"},

"authenticity_token": ""}

headers = {

'user-agent':'Mozilla/5.0',

'x-requested-with': 'XMLHttpRequest'

}

with requests.Session() as s:

res = s.get('https://www.instacart.com/',headers={'user-agent':'Mozilla/5.0'})

soup = BeautifulSoup(res.text, 'lxml')

token = soup.select_one("[name='csrf-token']").get('content')

data["authenticity_token"] = token

s.post("https://www.instacart.com/accounts/login",json=data,headers=headers)

resp = s.get(data_url, headers=headers)

for item in resp.json()['module_data']['items']:

name = item['name']

quantity = item['size']

price = item['pricing']['price']

product_page = baseurl + item['click_action']['data']['container']['path']

print(f'{name}\n{quantity}\n{price}\n{product_page}\n')

部分输出:

^{pr2}$

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果使用BeautifulSoup解析HTML代码时无法获取页面元素,可能是以下几个原因: 1. 页面元素是通过JavaScript动态生成的,而BeautifulSoup无法解析JavaScript代码。这种情况下可以尝试使用selenium等工具模拟浏览器操作,等待元素加载完成后再获取。 2. 页面元素是在iframe或者frame中嵌套的,而BeautifulSoup默认只能解析主文档中的元素。这种情况下可以尝试使用BeautifulSoup中的find_all方法的recursive参数设置为True,以便递归查所有iframe或者frame中的元素。 3. 页面元素的位置、属性等信息有误,导致无法匹配到元素。这种情况下可以尝试使用浏览器的开发者工具查看页面元素的具体信息,然后再进行匹配。 下面是一个使用BeautifulSoup递归查所有iframe或者frame中的元素的示例代码: ``` from bs4 import BeautifulSoup import requests # 发送请求获取HTML代码 url = 'http://example.com' response = requests.get(url) html = response.text # 解析HTML代码 soup = BeautifulSoup(html, 'html.parser') # 查所有iframe和frame中的元素 elements = soup.find_all(recursive=True) for element in elements: print(element) ``` 在上面的代码中,我们首先发送请求获取HTML代码,然后使用BeautifulSoup解析HTML代码。接着,我们使用find_all方法的recursive参数设置为True,以便递归查所有iframe或者frame中的元素。最后,我们打印出所有到的元素。通过以上方法,我们可以递归查所有iframe或者frame中的元素
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值