python爬虫电商京东_python爬虫爬取京东商品信息

#!/usr/bin/env python#-*- coding: utf-8 -*-#@Time : 2020/5/7 11:17#@Author : dddchongya#@Site :#@File : ComputerFromJD.py#@Software: PyCharm

importrequestsfrom bs4 importBeautifulSoup as bstimportjsonimportos

informationnumber=0defGetComment(id):

param={'callback': 'fetchJSON_comment98','productId': id,'score': 0,'sortType': 5,'page': 1,'pageSize': 10,'isShadowSku': 0,'rid': 0,'fold': 1,

}

url="https://club.jd.com/comment/productPageComments.action"headers={'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36',#标记了请求从什么设备,什么浏览器上发出

}

CommentLs={}

bool=1label=[]

comments=[]

commentnumber={}for i in range(1,5):

param["page"]=i

res_songs= requests.get(url, params=param, headers=headers)

jsondata=res_songs.text

jsondata= json.loads(jsondata.replace("(", "").replace(")", "").replace("fetchJSON_comment98", "").replace(" ","").replace(";", ""))if bool ==1:#标签只用拿一次

hotCommentTagStatistics=jsondata["hotCommentTagStatistics"]for j inhotCommentTagStatistics:

label.append(j["name"]+":"+str(j["count"]))#评论数量也只用拿一次

productCommentSummary = jsondata["productCommentSummary"]

commentnumber["commentCount"]=productCommentSummary["commentCount"]

commentnumber["defaultGoodCount"] = productCommentSummary["defaultGoodCount"]

commentnumber["goodCount"] = productCommentSummary["goodCount"]

commentnumber["poorCount"] = productCommentSummary["poorCount"]

commentnumber["generalCount"] = productCommentSummary["generalCount"]

commentnumber["afterCountStr"] = productCommentSummary["afterCount"]

commentnumber["showCount"] = productCommentSummary["showCount"]

bool=bool+1comment=jsondata["comments"]for j incomment:

comments.append(j["content"].replace("\n",""))

CommentLs["commentnumber"]=commentnumber

CommentLs["label"]=label

CommentLs["comments"]=commentsreturnCommentLsdefGetMoreInformation(id):

url="https://item.jd.com/"+id+".html"headers={'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36',#标记了请求从什么设备,什么浏览器上发出

}

res= requests.get(url, headers=headers)

html=bst(res.content)defGetGoodResone(LsComputer):

labells=[]

label=set()

labellist={}for i inLsComputer:if (i['comments']['commentnumber']['goodCount'] + i['comments']['commentnumber']['defaultGoodCount']) / float(i['comments']['commentnumber']['commentCount']) > 0.7:

labells.append(i['comments']["label"])for i inlabells:for j ini:

label.add(j.split(":")[0])for i inlabel:

labellist[i]=0for j inlabells:for k inj:

labellist[k.split(":")[0]] = labellist[k.split(":")[0]] + float(k.split(":")[1])

result= sorted(labellist.items(), key=lambda x: x[1], reverse=False)

with open(os.getcwd()+ '\好评过七十的标签排行.txt', 'w', encoding="utf-8") as f:for i inresult:

f.write(str(i))

f.write('\r\n')

f.close()defGetMaxSalesShop(LsComputer):

shop=set()for i inLsComputer:

shop.add(i["ShopName"])

shopcount={}

shopsalecount={}

shopprice={}for i inshop:

shopcount[i]=0

shopsalecount[i]=0

shopprice[i]=[]for i inshop:for j inLsComputer:if j["ShopName"]==i:if j["Price"].__len__()>=5:

price=j["Price"][0:-3].replace("\n","").replace(" ","").replace("\t","")#销售额

shopcount[i]=shopcount[i]+j["comments"]["commentnumber"]["commentCount"]*float(price)#价格总和,为了求平均数

shopprice[i].append(price)#销售量

shopsalecount[i]=shopsalecount[i]+j["comments"]["commentnumber"]["commentCount"]

shopprice2={}for i inshopprice:

sum=0if shopprice[i].__len__() !=0:for j inshopprice[i]:

sum=sum+float(j)

price=sum/(shopprice[i].__len__())

shopprice2[i]=priceprint()print()

result=sorted(shopcount.items(), key=lambda x: x[1], reverse=False)print("销售额排行::")for i inresult:print(i)

with open(os.getcwd()+ '\销售额排行.txt', 'w', encoding="utf-8") as f:for i inresult:

f.write(str(i))

f.write('\r\n')

f.close()print()print()

result= sorted(shopprice2.items(), key=lambda x: x[1], reverse=False)print("销售量排行::")for i inresult:print(i)

with open(os.getcwd()+ '\销售量排行.txt', 'w', encoding="utf-8") as f:for i inresult:

f.write(str(i))

f.write('\r\n')

f.close()print()print()

result= sorted(shopsalecount.items(), key=lambda x: x[1], reverse=False)print("平均价格排行::")for i inresult:print(i)

with open(os.getcwd()+ '\平均价格排行.txt', 'w', encoding="utf-8") as f:for i inresult:

f.write(str(i))

f.write('\r\n')

f.close()#可任意写搜索链接

url = 'https://search.jd.com/Search?keyword=%E7%94%B5%E8%84%91&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&wq=%E7%94%B5%E8%84%91&page='headers={'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36',#标记了请求从什么设备,什么浏览器上发出

}#伪装请求头

LsComputer=[]#bool=1 # 每页开头第一个商品格式有误差,所以以此为判断符号跳过第一个

for k in range(1,10):

url=url+str(k*2+1)

res= requests.get(url, headers=headers)

html=bst(res.content)

list=html.findAll("li",{"class","gl-item gl-item-presell"})for html inlist:

ComputerInformation={}

CustomUrl=html.find("div",{"class","p-img"}).find("a").get("href")if not str(CustomUrl).__contains__("https:"):

CustomUrl="https:"+CustomUrl#print(CustomUrl)

id=html.find("div",{"class","p-price"}).find("strong").get("class")

id=id[0].replace("J","").replace("_","")#拿到评论信息

Comments=GetComment(id)#print(Comment)

#进入页面拿更详细的信息

ImgUrl="https:"+str(html.find("div",{"class","p-img"}).find("img").get("source-data-lazy-img"))#print(ImgUrl)

Price=str(html.find("div",{"class","p-price"}).find("i"))[3:-4]#print(Price[3:-4])

Describe=str(html.find("div",{"class","p-name p-name-type-2"}).find("em").getText())#print(Describe)

#第一行一个会为空

ShopName=html.find("div",{"class","p-shop"}).find("a")if ShopName !=None:

ShopName=str(ShopName.getText())#print(ShopName)

#店铺描述可能有多个

Mode=html.find("div",{"class","p-icons"}).findAll("i")

BusinessMode=[]for i inMode:

BusinessMode.append(i.getText())#print(BusinessMode)

ComputerInformation["CustomUrl"]=CustomUrl

ComputerInformation["ImgUrl"] =ImgUrl

ComputerInformation["Price"] =Price

ComputerInformation["Describe"] =Describe

ComputerInformation["ShopName"] =ShopName

ComputerInformation["CustomUrl"] =CustomUrl

ComputerInformation["BusinessMode"] =BusinessMode

ComputerInformation["comments"]=Comments

LsComputer.append(ComputerInformation)for k in range(1,10):

url=url+str(k*2+1)

res= requests.get(url, headers=headers)

html=bst(res.content)

list=html.findAll("li",{"class","gl-item"})for html inlist:

ComputerInformation={}

CustomUrl=html.find("div",{"class","p-img"}).find("a").get("href")if not str(CustomUrl).__contains__("https:"):

CustomUrl="https:"+CustomUrl#print(CustomUrl)

id=html.find("div",{"class","p-price"}).find("strong").get("class")

id=id[0].replace("J","").replace("_","")#拿到评论信息

Comments=GetComment(id)#print(Comment)

#进入页面拿更详细的信息

ImgUrl="https:"+str(html.find("div",{"class","p-img"}).find("img").get("source-data-lazy-img"))#print(ImgUrl)

Price=str(html.find("div",{"class","p-price"}).find("i"))[3:-4]#print(Price[3:-4])

Describe=str(html.find("div",{"class","p-name p-name-type-2"}).find("em").getText())#print(Describe)

#第一行一个会为空

ShopName=html.find("div",{"class","p-shop"}).find("a")if ShopName !=None:

ShopName=str(ShopName.getText())#print(ShopName)

#店铺描述可能有多个

Mode=html.find("div",{"class","p-icons"}).findAll("i")

BusinessMode=[]for i inMode:

BusinessMode.append(i.getText())#print(BusinessMode)

ComputerInformation["CustomUrl"]=CustomUrl

ComputerInformation["ImgUrl"] =ImgUrl

ComputerInformation["Price"] =Price

ComputerInformation["Describe"] =Describe

ComputerInformation["ShopName"] =ShopName

ComputerInformation["CustomUrl"] =CustomUrl

ComputerInformation["BusinessMode"] =BusinessMode

ComputerInformation["comments"]=Comments

LsComputer.append(ComputerInformation)#数据写入文件

with open(os.getcwd() + '\json.txt', 'w',encoding="utf-8") as f:for i inLsComputer:

f.write(json.dumps(i,indent=4,ensure_ascii=False))

f.close()

GetMaxSalesShop(LsComputer)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
爬虫(Web Crawler)是一种自动化程序,用于从互联网上收集信息。其主要功能是访问网页、提取数据并存储,以便后续分析或展示。爬虫通常由搜索引擎、数据挖掘工具、监测系统等应用于网络数据抓取的场景。 爬虫的工作流程包括以下几个关键步骤: URL收集: 爬虫从一个或多个初始URL开始,递归或迭代地发现新的URL,构建一个URL队列。这些URL可以通过链接分析、站点地图、搜索引擎等方式获取。 请求网页: 爬虫使用HTTP或其他协议向目标URL发起请求,获取网页的HTML内容。这通常通过HTTP请求库实现,如Python中的Requests库。 解析内容: 爬虫对获取的HTML进行解析,提取有用的信息。常用的解析工具有正则表达式、XPath、Beautiful Soup等。这些工具帮助爬虫定位和提取目标数据,如文本、图片、链接等。 数据存储: 爬虫将提取的数据存储到数据库、文件或其他存储介质中,以备后续分析或展示。常用的存储形式包括关系型数据库、NoSQL数据库、JSON文件等。 遵守规则: 为避免对网站造成过大负担或触发反爬虫机制,爬虫需要遵守网站的robots.txt协议,限制访问频率和深度,并模拟人类访问行为,如设置User-Agent。 反爬虫应对: 由于爬虫的存在,一些网站采取了反爬虫措施,如验证码、IP封锁等。爬虫工程师需要设计相应的策略来应对这些挑战。 爬虫在各个领域都有广泛的应用,包括搜索引擎索引、数据挖掘、价格监测、新闻聚合等。然而,使用爬虫需要遵守法律和伦理规范,尊重网站的使用政策,并确保对被访问网站的服务器负责。
爬虫(Web Crawler)是一种自动化程序,用于从互联网上收集信息。其主要功能是访问网页、提取数据并存储,以便后续分析或展示。爬虫通常由搜索引擎、数据挖掘工具、监测系统等应用于网络数据抓取的场景。 爬虫的工作流程包括以下几个关键步骤: URL收集: 爬虫从一个或多个初始URL开始,递归或迭代地发现新的URL,构建一个URL队列。这些URL可以通过链接分析、站点地图、搜索引擎等方式获取。 请求网页: 爬虫使用HTTP或其他协议向目标URL发起请求,获取网页的HTML内容。这通常通过HTTP请求库实现,如Python中的Requests库。 解析内容: 爬虫对获取的HTML进行解析,提取有用的信息。常用的解析工具有正则表达式、XPath、Beautiful Soup等。这些工具帮助爬虫定位和提取目标数据,如文本、图片、链接等。 数据存储: 爬虫将提取的数据存储到数据库、文件或其他存储介质中,以备后续分析或展示。常用的存储形式包括关系型数据库、NoSQL数据库、JSON文件等。 遵守规则: 为避免对网站造成过大负担或触发反爬虫机制,爬虫需要遵守网站的robots.txt协议,限制访问频率和深度,并模拟人类访问行为,如设置User-Agent。 反爬虫应对: 由于爬虫的存在,一些网站采取了反爬虫措施,如验证码、IP封锁等。爬虫工程师需要设计相应的策略来应对这些挑战。 爬虫在各个领域都有广泛的应用,包括搜索引擎索引、数据挖掘、价格监测、新闻聚合等。然而,使用爬虫需要遵守法律和伦理规范,尊重网站的使用政策,并确保对被访问网站的服务器负责。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值