python 爬虫爬取京东ps4售卖情况
作者:雾霾王者 时间:2022-05-19 01:35:28
代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File : HtmlParser.py
# @Author: 赵路仓
# @Date : 2020/3/17
# @Desc :
# @Contact : 398333404@qq.com
import json
from lxml import etree
import requests
from bs4 import BeautifulSoup
url="https://search.jd.com/Search?keyword=ps4&enc=utf-8&wq=ps4&pvid=cf0158c8664442799c1146a461478c9c"
head={
'authority': 'search.jd.com',
'method': 'GET',
'path': '/s_new.php?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&wq=%E6%89%8B%E6%9C%BA&cid2=653&cid3=655&page=4&s=84&scrolling=y&log_id=1529828108.22071&tpl=3_M&show_items=7651927,7367120,7056868,7419252,6001239,5934182,4554969,3893501,7421462,6577495,26480543553,7345757,4483120,6176077,6932795,7336429,5963066,5283387,25722468892,7425622,4768461',
'scheme': 'https',
'referer': 'https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&wq=%E6%89%8B%E6%9C%BA&cid2=653&cid3=655&page=3&s=58&click=0',
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36',
'x-requested-with': 'XMLHttpRequest',
}
def page(page):
print("开始")
url = "https://search.jd.com/Search?keyword=ps4&enc=utf-8&qrst=1&rt=1&stop=1&vt=1&wq=ps4&page="+page+"&s=181&click=0"
r=requests.get(url,timeout=3,headers=head)
r.encoding=r.apparent_encoding
# print(r.text)
b=BeautifulSoup(r.text,"html.parser")
#print(b.prettify())
_element = etree.HTML(r.text)
datas = _element.xpath('//li[contains(@class,"gl-item")]')
print(datas)
for data in datas:
p_price = data.xpath('div/div[@class="p-price"]/strong/i/text()')
p_comment = data.xpath('div/div[5]/strong/a/text()')
p_name = data.xpath('div/div[@class="p-name p-name-type-2"]/a/em/text()')
p_href = data.xpath('div/div[@class="p-name p-name-type-2"]/a/@href')
comment=' '.join(p_comment)
name = ' '.join(p_name)
price = ' '.join(p_price)
href = ' '.join(p_href)
print(name,price,p_comment,href)
if __name__=="__main__":
page("5")
爬取结果
来源:https://www.cnblogs.com/zlc364624/p/12874090.html
标签:python,爬虫,京东,ps4
0
投稿
猜你喜欢
CSS框架带来的效率提升
2007-12-27 20:01:00
golang常用库之操作数据库的orm框架-gorm基本使用详解
2024-01-28 21:22:19
Python进度条的制作代码实例
2022-01-01 23:17:34
python实现输入三角形边长自动作图求面积案例
2023-08-12 01:47:49
vue实现瀑布流组件滑动加载更多
2024-05-02 17:09:45
RDFa介绍——构建更友好的web页面
2009-09-19 17:01:00
mysql 8.0.22压缩包完整安装与配置教程图解(亲测安装有效)
2024-01-25 05:26:14
基于Python爬取51cto博客页面信息过程解析
2023-06-11 16:27:37
可能是最通俗的一篇介绍markdown的文章
2022-08-09 09:48:43
Jupyter notebook中5个有趣的魔法命令分享
2021-11-28 23:26:30
一文读懂吞吐量(TPS)、QPS、并发数、响应时间(RT)概念
2022-04-06 14:58:25
使用OpenCV对运动员的姿势进行检测功能实现
2022-06-08 03:23:14
加密SQL Anywhere 提升政府行业数据安全
2008-12-03 15:25:00
python pygame实现滚动横版射击游戏城市之战
2021-07-11 00:32:51
python打造爬虫代理池过程解析
2021-10-14 23:43:44
vscode配置anaconda3的方法步骤
2023-11-05 16:45:02
Python调用ctypes使用C函数printf的方法
2022-06-03 22:09:43
python基础教程之Hello World!
2021-03-05 22:45:06
超详细注释之OpenCV制作图像Mask
2021-10-20 14:15:34
tensorflow实现打印ckpt模型保存下的变量名称及变量值
2021-07-02 21:19:25