使用python抓取AJAX电子商务网站 [英] Scraping AJAX e-commerce site using python

查看:143
本文介绍了使用python抓取AJAX电子商务网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用BeautifulSoup 抓取电子商务网站时遇到问题.我做了一些谷歌搜索,但仍然无法解决问题.

I have a problem on scraping an e-commerce site using BeautifulSoup. I did some Googling but I still can't solve the problem.

请参考图片:

这是我尝试抓取的网站:" https ://shopee.com.my/search?keyword = h370m "

问题:

  1. 当我尝试在Google Chrome(F12)上打开Inspect Element时,可以看到产品名称,价格等信息.但是,当我运行python程序时,无法获得相同的代码,并且标记在python结果中.经过一番谷歌搜索,我发现该网站使用AJAX查询来获取数据.

任何人都可以通过抓取AJAX网站来帮助我找到获取这些产品数据的最佳方法吗?我想以表格形式显示数据.

Anyone can help me on the best methods to get these product's data by scraping an AJAX site? I would like to display the data in a table form.

我的代码:

import requests
from bs4 import BeautifulSoup
source = requests.get('https://shopee.com.my/search?keyword=h370m')
soup = BeautifulSoup(source.text, 'html.parser')
print(soup)

推荐答案

欢迎使用StackOverflow!您可以检查将ajax请求发送到的位置并进行复制.

Welcome to StackOverflow! You can inspect where the ajax request is being sent to and replicate that.

在这种情况下,请求 fake-useragent 之类的包,也可以仅硬编码该代理的字符串.

In this case the request goes to this api url. You can then use requests to perform a similar request. Notice however that this api endpoint requires a correct UserAgent header. You can use a package like fake-useragent or just hardcode a string for the agent.

import requests

# fake useragent
from fake_useragent import UserAgent
user_agent = UserAgent().chrome

# or hardcode
user_agent = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1468.0 Safari/537.36'

url = 'https://shopee.com.my/api/v2/search_items/?by=relevancy&keyword=h370m&limit=50&newest=0&order=desc&page_type=search'
resp = requests.get(url, headers={
    'User-Agent': user_agent
})
data = resp.json()
products = data.get('items')

这篇关于使用python抓取AJAX电子商务网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆