当前位置: 首页 > 科技观察

Python爬虫实战:采集淘宝商品信息并导入EXCEL表格

时间:2023-03-13 12:20:12 科技观察

文章目录前言1.分析淘宝URL的构成2.查看网页源码,用re库提取信息1.查看源码二.从re库中提取信息三:填写函数四:填写主要函数五:完整代码前言本文简单使用python的requests库和re正则表达式爬取淘宝的商品信息(商品名称,商品价格、产区、销量),最后使用xlsxwriter库将信息放入Excel表中。最终效果图如下:提示:以下为本文正文内容1、分析淘宝网址的构成1、我们的第一个需求是输入商品名称,并返回相应的信息,所以这里选择一个商品观察它的URL,这里我们选择书包,打开网页,可以看到它的URL是:https://s.taobao.com/search?q=%E4%B9%A6%E5%8C%85&imgfile=&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm=a21bo.2017.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170306单从这个url我们可能看不出什么,但是从图中我们可以看到一些端倪。我们发现q后面的参数是我们要获取的item的名称2.我们的第二个需求是根据输入的编号爬取item的页码,所以接下来我们观察下几页url的组成,所以我们可以得出分页是根据最后s的值=(44(页数-1))2.查看网页源码,用re库提取信息1.查看源码而这里的信息就是我们需要的2.从re库中提取信息a=re.findall(r'"raw_title":"(.*?)"',html)b=re.findall(r'"view_price":"(.*?)"',html)c=re.findall(r'"item_loc":"(.*?)"',html)d=re.findall(r'"view_sales":"(.*?)"',html)三:填写函数这里我写了三个函数,第一个函数来获取html网页,代码如下:defGetHtml(url):r=requests.get(url,headers=headers)r.raise_for_status()r.encoding=r.apparent_encodingreturnr获取网页的第二个URL代码如下:defGeturls(q,x):url="https://s.taobao.com/search?q="+q+"&imgfile=&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm"\"=a21bo.2017.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170306"urls=[]urls.append(url)ifx==1:returnurlsforiinrange(1,x):url="https://s.taobao.com/search?q="+q+"&commend=all&ssid=s5-e&search_type=item"\"&sourceId=tb.index&spm=a21bo.2017.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170306"\"&bcoffset=3&ntoffset=3&p4ppushleft=1%2C48&s="+str(i*44)urls.append(url)returnurls使用第三个for获取我们需要的产品信息并写入Excel表格的代码如下:defGetxxintoExcel(html):globalcount#定义一个全局变量count,用于稍后填写excel表格a=re.findall(r'"raw_title":"(.*?)"',html)#(.*?)匹配任何字符b=re.findall(r'"view_price":"(.*?)"',html)c=re.findall(r'"item_loc":"(.*?)"',html)d=re.findall(r'"view_sales":"(.*?)"',html)x=[]foriinrange(len(a)):try:x.append((a[i],b[i],c[i],d[i]))#得到得到的信息放入新列表exceptIndexError:break=0foriinrange(len(x)):worksheet.write(count+i+1,0,x[i][0])#worksheet.write方法用于写入数据,第一个数字是行位置,第二个数字是列,第三个是写入的数据信息worksheet.write(count+i+1,1,x[i][1])worksheet.write(count+i+1,2,x[i][2])worksheet.write(count+i+1,3,x[i][3])count=count+len(x)#下次写入的行数是本次长度+1returnprint("completed")4:main函数填写if__name__=="__main__":count=0headers={"user-agent":"Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/80.0.3987.149Safari/537.36","cookie":""#cookie因人而异,因为有反爬机制,如果爬的太快,你可能稍后必须刷新它自己的cookie。}q=input("输入商品")x=int(input("你要抓取多少页"))urls=Geturls(q,x)workbook=xlsxwriter.Workbook(q+".xlsx")worksheet=工作簿。add_worksheet()worksheet.set_column('A:A',70)worksheet.set_column('B:B',20)worksheet.set_column('C:C',20)worksheet.set_column('D:D',20)worksheet.write('A1','Name')worksheet.write('B1','Price')worksheet.write('C1','Region')worksheet.write('D1','付款编号')forurlinurls:html=GetHtml(url)s=GetxxintoExcel(html.text)time.sleep(5)workbook.close()#程序结束前不要打开excel,excel表在当前目录5:完成代码importreimportrequestsimportxlsxwriterimporttimedefGetxxintoExcel(html):globalcounta=re.findall(r'"raw_title":"(.*?)"',html)b=re.findall(r'"view_price":"(.*?)"',html)c=re.findall(r'"item_loc":"(.*?)"',html)d=re.findall(r'"view_sales":"(.*?)"',html)x=[]foriinrange(len(a)):try:x.append((a[i],b[i],c[i],d[i]))exceptIndexError:break=0foriinrange(len(x)):worksheet.write(计数+i+1,0,x[i][0])worksheet.write(count+i+1,1,x[i][1])worksheet.write(count+i+1,2,x[i][2])worksheet.write(count+i+1,3,x[i][3])count=count+len(x)returnprint("已完成")defGeturls(q,x):url="https://s.taobao.com/search?q="+q+"&imgfile=&commend=all&ssid=s5-e&search_type=item&sourceId=tb.index&spm"\"=a21bo.2017.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170306"urls=[]urls.append(url)ifx==1:returnurlsforiinrange(1,x):url="https://s.taobao.com/search?q="+q+"&commend=all&ssid=s5-e&search_type=item"\"&sourceId=tb.index&spm=a21bo.2017.201856-taobao-item.1&ie=utf8&initiative_id=tbindexz_20170306"\"&bcoffset=3&ntoffset=3&p4ppushleft=1%2C48&s="+str(i*44)urls.append(url)returnurlsdefGetHtml(url):r=requests.get(url,headers=headers)r.raise_for_status()r.encoding=r.apparent_encodingreturnrif__name__=="__main__":count=0headers={"user-agent":"Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/80.0.3987.149Safari/537.36","cookie":""}q=input("输入商品")x=int(input("你要抓取多少页"))urls=Geturls(q,x)workbook=xlsxwriter.Workbook(q+".xlsx")worksheet=workbook.add_worksheet()worksheet.set_column('A:A',70)worksheet.set_column('B:B',20)worksheet.set_column('C:C',20)worksheet.set_column('D:D',20)worksheet.write('A1','Name')worksheet.write('B1','Price')worksheet.write('C1','Region')worksheet.write('D1','NumberofPayments')xx=[]forurlinurls:html=GetHtml(url)s=GetxxintoExcel(html.text)time.sleep(5)workbook.close()【编辑推荐】红帽开放混合云助力企业成为数字原生企业分析鸿蒙系统helloworld程序是怎么调用的?SYS_RUN是干什么的?为什么5G突然“不流行”了?新方向、新特性:Python3.9完整版来了,请停止在Python中无休止地使用列表