难点:

  • 1,京东首次搜索只展示30条数据,这个可以直接在源代码取到,但是也要注意不同页面抓取规则可能不一样(页面结构有变化需要判断)

  • 2,继续下拉可以在ajax获取到另外30条数据,但是这个requests提交需要各种参数,很麻烦,我这暂时没有找到自动填写的方法,只能根据搜索需求人工改写下

  • 解析页面信息有好多坑,比如有的价格不全,同一个页面需要解析的规则就不一样

  • ajax参数,headers的path和referer和data的login_id和show_time这是在前30个上篇页代码里提取到拼凑的



代码:

import requests
from  lxml import etree
import rekeyword=input("请输入查询商品:")
print(type(keyword))
g=[]
for i in range(1,2):url="https://search.jd.com/Search?keyword={}&enc=utf-8&page={}".format(keyword,i*2-1)header={# ":authority":"search.jd.com",# ":method":"GET",# ":scheme":" https","upgrade - insecure - requests": "1","user-agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36"}html=requests.get(url=url,headers=header)html.encoding="utf-8"# print(html.text)html=html.textnewhtml=etree.HTML(html)print(newhtml)log_id=re.findall("log_id:'(.*?)'",html,re.S)[0]cid=re.findall("LogParm.*?cid:(.*?),",html,re.S)[0]print(cid)sku_id=newhtml.xpath('//*[@id="J_goodsList"]/ul/li/@data-sku')p_list=",".join('%s' % id for id in sku_id)print(sku_id)print(p_list)if len(newhtml.xpath('//*[@id="J_goodsList"]/ul/li//div[@class="p-price"]/strong/i/text()'))==30:img_url=newhtml.xpath('//div[@id="J_goodsList"]/ul/li//div[@class="p-img"]//img/@source-data-lazy-img')print(img_url)price=newhtml.xpath('//*[@id="J_goodsList"]/ul/li//div[@class="p-price"]/strong/i/text()')title=newhtml.xpath('//*[@id="J_goodsList"]/ul/li//div[contains(@class,"p-name")]/a/em/text()')#.strip()# title="".join(title)# title=title.split(":")product_url=newhtml.xpath('//*[@id="J_goodsList"]/ul/li//div[contains(@class,"p-name")]//a/@href')# commit=html.xpath('//*[@id="J_goodsList"]/ul/li[1]/div/div[4]/strong/text()')#.strip()commit=newhtml.xpath('//*[@id="J_goodsList"]/ul/li//div[@class="p-commit"]/strong/a/text()')#.strip()# shop_name=newhtml.xpath('//*[@id="J_goodsList"]/ul/li//div[@class="p-shop"]/span/a/text()')for i in range(30):list_1=[keyword,"https:"+product_url[i],price[i],commit[i],"http:"+img_url[i]]print(list_1)g.append(list_1)else:for n in range(30):if newhtml.xpath('//*[@id="J_goodsList"]/ul/li[%d]//div[@class="p-price"]/strong/i/text()'%(n+1)):img_url = newhtml.xpath('//div[@id="J_goodsList"]/ul/li[%d]//div[@class="p-img"]//img/@source-data-lazy-img'%(n+1))# print(img_url)price = newhtml.xpath('//*[@id="J_goodsList"]/ul/li[%d]//div[@class="p-price"]/strong/i/text()'%(n+1))title = newhtml.xpath('//*[@id="J_goodsList"]/ul/li[%d]//div[contains(@class,"p-name")]/a/em/font[1]/text()'%(n+1))  # .strip()# title="".join(title)# title=title.split(":")product_url = newhtml.xpath('//*[@id="J_goodsList"]/ul/li[%d]//div[contains(@class,"p-name")]//a/@href'%(n+1))# commit=html.xpath('//*[@id="J_goodsList"]/ul/li[1]/div/div[4]/strong/text()')#.strip()commit = newhtml.xpath('//*[@id="J_goodsList"]/ul/li[%d]//div[@class="p-commit"]/strong/a/text()'%(n+1))  # .strip()list_2 = [keyword , "https:"+product_url[0], price[0], commit[0], "http:"+img_url[0]]print(list_2)g.append(list_2)else:img_url = newhtml.xpath('//div[@id="J_goodsList"]/ul/li[%d]//div[@class="p-img"]//img/@source-data-lazy-img'%(n+1))# print(img_url)price = newhtml.xpath('//*[@id="J_goodsList"]/ul/li[%d]//div[@class="p-price"]/strong/@data-price'%(n+1))title = newhtml.xpath('//*[@id="J_goodsList"]/ul/li[%d]//div[contains(@class,"p-name")]/a/em/font[1]/text()'%(n+1))  # .strip()# title="".join(title)# title=title.split(":")product_url = newhtml.xpath('//*[@id="J_goodsList"]/ul/li[%d]//div[contains(@class,"p-name")]//a/@href'%(n+1))# commit=html.xpath('//*[@id="J_goodsList"]/ul/li[1]/div/div[4]/strong/text()')#.strip()commit = newhtml.xpath('//*[@id="J_goodsList"]/ul/li[%d]//div[@class="p-commit"]/strong/a/text()'%(n+1))  # .strip()list_3 = [keyword , "https:"+product_url[0], price[0], commit[0], "http:"+img_url[0]]print(list_3)g.append(list_3)header1={
'authority': 'search.jd.com',
'method': 'GET',
'scheme': 'https',
"path": "/s_new.php?keyword=%E5%A5%B3%E9%9E%8B&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&wq=%E5%A5%B3%E9%9E%8B&cid2=11731&page=2&s=27&scrolling=y&log_id=1541499650.39561&tpl=3_M",
"referer": "https://search.jd.com/Search?keyword=%E5%A5%B3%E9%9E%8B&enc=utf-8&wq=%E5%A5%B3%E9%9E%8B&pvid=11f0d7bbd549489ea0ff9c18280008e3",
"user-agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36",
"x-requested-with": "XMLHttpRequest",
'Cookie': '__jdv=122270672|direct|-|none|-|1541463047359; __jdc=122270672; __jdu=15414630473591816564566; PCSYCityID=country_2468; shshshfpa=06433229-fec9-71f1-ee80-5e4d2053f3b2-1541463051; xtest=1192.cf6b6759; ipLoc-djd=1-72-2799-0; shshshfpb=29d35c4b6bd0a4874aff06fde9a21bb415be00d7767a8105a1c51da983; rkv=V0000; qrsc=3; mt_xid=V2_52007VwMWV11dVVgeTB9eAW8DG1JaXFVfG04ebFVuVkJQVVFSRh5NSgsZYgERB0FQW1gYVRsJAjcFFFZZWQAKGHkaXQVuHxJSQVlVSx5AElgFbAcbYl9oUmocThBdBWAFE1RtWFdcGA%3D%3D; 3AB9D23F7A4B3C9B=3DVBCHQ2ZQDBE7WHQNBTXMZIG2LRSITXIEP5G2KLLX7F665PL45NH2F4HIBZ7GYW7TBTBPQEGC27GWLCFQV3UVL2EQ; _gcl_au=1.1.201025286.1541468212; shshshfp=72033013163d2ec74f4450e6f7114c1a; __jda=122270672.15414630473591816564566.1541463047.1541468202.1541471285.4; wlfstk_smdl=607dk6qcnoo82vqtspoz07g6hf0a8h6f; TrackID=1aDczZLZIOi53VMMgAJw6R6jU_JwW0j0Q3kPXr2DBxehnhKeoPkixGxlJ1XFOqdIqsW5IHw3HorqriaLnpP7qx_rF45aE522LK_J72xHV0XU; thor=A2F041014FF97AD3CBE36A18D7A197BD280A27D92876522948662EEBEF7FAAE9D4EE69FADC66DD46EC5FB2DA15E3A77A2B031AB32800A19FDD8BF76438EC46467045B795A654A74E62B5D2C1BEE34F0566FBA73C6ADB9AE74640B83FFF64DB25EF4E84890A70EC7A2A054562CA4A906EBC3E8B8DE2E06A32A741577FBDE89130428D846DC18B195004A8AFE75665A1DA43AAC5AC651F19D0CCB3FDF2AD68D88A; pinId=Gs1wY_18rJHCJb2AcSkHc7V9-x-f3wj7; pin=jd_605257796165b; unick=%E6%B6%9F%E6%BC%AA%E5%9E%84; ceshi3.com=103; _tp=wwla5rjsr%2FuWFA%2FmLBXQrei5UhIKee6ThwQXFShjs60%3D; _pst=jd_605257796165b; __jdb=122270672.4.15414630473591816564566|4.1541471285; shshshsID=eaceb1587ae4eaba8646ed1b230970b4_2_1541472058709'}
url1="https://search.jd.com/s_new.php?keyword=%s&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&suggest=7.def.0.V00&cid2=%s&page=2&s=28&scrolling=y&log_id=%s&tpl=3_M&show_items=%s"%(str(keyword),cid,log_id,p_list)
html1=requests.get(url=url1,headers=header1)
html1.encoding="utf-8"
html2=html1.text
# print(html2)
html3=etree.HTML(html2)
product_url=html3.xpath('//div[contains(@class,"p-name")]//a/@href')
price=html3.xpath('//div[@class="p-price"]/strong/i/text()')
commit=html3.xpath('//div[@class="p-commit"]/strong/a/text()')
img_ul=html3.xpath('//div[@class="p-img"]//img/@source-data-lazy-img')
title=html3.xpath('//div[contains(@class,"p-name")]/a/em')for i in range(30):list_4 = [keyword, "https:" + product_url[i], price[i], commit[i], "http:" + img_url[i]]print(list_4)g.append(list_4)print(url1)print(g)
print(len(g))

爬虫-京东商城商品搜索页爬取相关推荐

  1. 使用HttpClient和Jsoup爬取京东商城关键字搜索的商品页面

    使用HttpClient可以创建浏览器对象,然后对页面数据进行抓取,另外再使用Jsoup库解析Html页面.因为Jsoup和JQuery一样有选择器获取元素的方式,所以抓取页面数据变得非常方便. 例如 ...

  2. Python爬虫教程:Python爬取京东商城商品大图详解

    Python爬取京东商城商品大图详解 做为一个爬虫初学者,在做爬取网址图片的练习中以京东网为例爬取商品大图并保存在相应的文件夹 1.导入模块 import urllib.request import ...

  3. python翻页爬取京东商品评价

    python翻页爬取京东商品评价 以小米手机评论为案例 1.找到目标的url 2.检查响应结果 3.解析json数据,发现相应数据无法成功解析 注意:json数据一般都是以大括号或中括号开头或者结尾的 ...

  4. [Python Scrapy爬虫] 二.翻页爬取农产品信息并保存本地

    前面 "Python爬虫之Selenium+Phantomjs+CasperJS" 介绍了很多Selenium基于自动测试的Python爬虫程序,主要利用它的xpath语句,通过分 ...

  5. 爬虫如何翻页 爬取豆瓣排名250电影

    1.爬虫翻页问题: 正在做爬虫练习:爬取豆瓣电影排名前250的电影,但一页只显示排名前25的电影,爬取250部电影就遇到了爬虫翻页的问题.记录下,希望帮助到正在学习的小伙伴! 2.爬虫翻页解决思路: ...

  6. 微博关键词搜索并爬取前40页内容与图片

    微博关键词搜索并爬取前40页内容与图片 # -*- coding: utf-8 -*- """ @author: tanderick """ ...

  7. python爬取商品信息_python爬取商品信息

    原博文 2014-11-27 02:09 − 老严要爬某网购网站的商品信息,正好我最近在学python,就一起写了一个简单的爬虫程序. 需求:某网的商品信息,包括商品名,市场价和售价 工具:pytho ...

  8. 【爬虫】4.5 实践项目——爬取当当网站图书数据

    目录 1. 网站图书数据分析 2. 网站图书数据提取 3. 网站图书数据爬取 (1)创建 MySQL 数据库 (2)创建 scrapy 项目 (3)编写 items.py 中的数据项目类 (4)编写 ...

  9. python爬虫多久能学会-不踩坑的Python爬虫:如何在一个月内学会爬取大规模数据...

    原标题:不踩坑的Python爬虫:如何在一个月内学会爬取大规模数据 Python爬虫为什么受欢迎 如果你仔细观察,就不难发现,懂爬虫.学习爬虫的人越来越多,一方面,互联网可以获取的数据越来越多,另一方 ...

  10. Python网络爬虫数据采集实战:Scrapy框架爬取QQ音乐存入MongoDB

    ​    通过前七章的学习,相信大家对整个爬虫有了一个比较全貌的了解 ,其中分别涉及四个案例:静态网页爬取.动态Ajax网页爬取.Selenium浏览器模拟爬取和Fillder今日头条app爬取,基本 ...

最新文章

  1. 云计算市场竞争激烈 亚马逊微软IBM阿里业绩亮眼
  2. win服务器系统程序原因
  3. Coolite Toolkit学习笔记九:表单布局控件FormLayout与FromPanel
  4. js foreach用法_使用 nodejs 写爬虫(一): 常用模块和 js 语法
  5. python如何安装wordcloud_不知如何利用Python中wordcloud的安装和使用?这里手把手教你...
  6. Mac系统下如何使用命令行方式启动MySQL
  7. const int *a和int*const a 的区别详解
  8. 刷新存储器的容量单位是什么_SRAM的容量扩展
  9. 豆瓣评分9.0以上,数据分析、爬虫、Python等书籍,45本包邮送到家!
  10. PHP导航栏管理,zblog导航栏管理设置的几种方法
  11. 【树上贪心】Tree with Small Distances【codeforces-Round #506-div3-E】
  12. java三大框架要学多久_新手学习SSH三大框架的几点建议
  13. STK和c语言集成开发,STKX组件实现c#与stk的集成.pdf
  14. 手把手教你使用《ProxyMan》抓取App接口
  15. 小程序开发工具命令行启动配置
  16. sublime 快捷键
  17. 晶振讲述工作原理及作用
  18. 用 Python 写软件原来这么简单!
  19. qcustomplot圆_Qt之QCustomPlot(图形库)
  20. 用机械键盘敲代码会不会更带感 ?送 9 套雷柏机械键盘

热门文章

  1. 使用transform使定位元素居中
  2. github克隆项目时遇到的一系列问题的解决方式
  3. 基于Python完成CA系统的设计和实现(附源码)
  4. android识别人脸并放贴纸,超简单集成HMS ML Kit 人脸检测实现可爱贴纸
  5. Show attend and Tell模型
  6. 春节恐归族:有多少人不敢回家过年 (l转)
  7. ElasticSearch全文搜索引擎-入门篇
  8. Android百度地图Poi检索开发总结
  9. 抖音、头条一个都不放过,字节小游戏上线攻略(续)!再送源码福利
  10. 深圳实验室装修室内总结方案