爬虫笔记:淘宝商品价格定向爬虫实例分析



功能描述:

目的:获取淘宝搜索页面的信息,提取其中的商品名称和价格

理解:淘宝的搜索接口、翻页处理

搜索接口与翻页的url对应属性:

Google Chrome上进入淘宝,搜索书包,点进商品页面,点击下一页

搜索书包的起始页面url:_https://s.taobao.com/search?q=%E4%B9%A6%E5%8C%85&imgfile=&js=1&stats_click=search_radio_all%3A1&initiative_id=staobaoz_20180710&ie=utf8

书包第二页:https://s.taobao.com/search?q=%E4%B9%A6%E5%8C%85&imgfile=&js=1&stats_click=search_radio_all%3A1&initiative_id=staobaoz_20180710&ie=utf8&bcoffset=3&ntoffset=3&p4ppushleft=1%2C48&s=44

程序的结构设计

步骤1:提交商品搜索请求,循环获取页面

步骤2:对于每个页面,提取商品名称和价格信息

步骤3:将信息输出到屏幕上

淘宝商品比价定向爬虫实例编写

  1. #CrowTaobaoPrice.py
  2. import requests
  3. import re
  4.  
  5. def getHTMLText(url):
  6.     try:
  7.         = requests.get(url, timeout=30)
  8.         r.raise_for_status()
  9.         r.encoding = r.apparent_encoding
  10.         return r.text
  11.     except:
  12.         return ""
  13.      
  14. def parsePage(ilt, html):
  15.     try:
  16.         plt = re.findall(r'\"view_price\"\:\"[\d\.]*\"',html)
  17.         tlt = re.findall(r'\"raw_title\"\:\".*?\"',html)
  18.         for in range(len(plt)):
  19.             price = eval(plt[i].split(':')[1])
  20.             title = eval(tlt[i].split(':')[1])
  21.             ilt.append([price , title])
  22.     except:
  23.         print("")
  24.  
  25. def printGoodsList(ilt):
  26.     tplt = "{:4}\t{:8}\t{:16}"
  27.     print(tplt.format("序号""价格""商品名称"))
  28.     count = 0
  29.     for in ilt:
  30.         count = count + 1
  31.         print(tplt.format(count, g[0], g[1]))
  32.          
  33. def main():
  34.     goods = '书包'
  35.     depth = 3
  36.     start_url = 'https://s.taobao.com/search?q=' + goods
  37.     infoList = []
  38.     for in range(depth):
  39.         try:
  40.             url = start_url + '&s=' + str(44*i)
  41.             html = getHTMLText(url)
  42.             parsePage(infoList, html)
  43.         except:
  44.             continue
  45.     printGoodsList(infoList)
  46.      
  47. main()

实例分析

  1. def getHTMLText(url):
  2.     try:
  3.         = requests.get(url, timeout=30)
  4.         r.raise_for_status()
  5.         r.encoding = r.apparent_encoding
  6.         return r.text
  7.     except:
  8.         return ""r

r = requests.get(url,timeout=30)

requests.get:获取html的主要方法(requests库的使用方法)

r=requests.get(url,params,**kwargs)

url: 需要爬取的网站地址。
params:    翻译过来就是参数, url中的额外参数,字典或者字节流格式,可选。
**kwargs :   12个控制访问的参数

**kwargs参数:

timeout:用于设定超时时间, 单位为秒,当发起一个get请求时可以设置一个timeout时间, 如果在timeout时间内请求内容没有返回, 将产生一个timeout的异常。

r.raise_for_status()

r.status_code:http请求的返回状态,若为200则表示请求成功。用r.raise_for_status() 语句去捕捉异常,该语句在方法内部判断r.status_code是否等于200,如果不等于,则抛出异常。

r.encoding = r.apparent_encoding

r.encoding:从http header 中猜测的相应内容编码方式.r.apparent_encoding:从内容中分析出的响应内容编码方式(备选编码方式)相等则表示:自动配置编码

  1. def parsePage(ilt, html):
  2.     try:
  3.         plt = re.findall(r'\"view_price\"\:\"[\d\.]*\"',html)
  4.         tlt = re.findall(r'\"raw_title\"\:\".*?\"',html)
  5.         for in range(len(plt)):
  6.             price = eval(plt[i].split(':')[1])
  7.             title = eval(tlt[i].split(':')[1])
  8.             ilt.append([price , title])
  9.     except:
  10.         print("")

re.findall(r'\"view price\"\:\"[\d\.]*\"',html)

re.findall():搜索字符串,以列表类型返回全部能匹配的子串,上式中“view price\”来自于:点进任一商品页面,右键查看源代码,商品价格的html属性为view price,商品名称的属性为raw title,‘\"’匹配双引号,‘view_price’匹配view_price,‘\:’匹配冒号,‘\d\.’匹配一个整数加一个小数点,‘[]*’*号匹配中括号内的正则表达式,商品名称同理,其中‘.*?’用于匹配前面频繁或重复出现符号的非贪婪版本,这里用于匹配最短数目的同性质字符,如下图。

(r'')r表示单引号内全为正则表达式符号,如\n要在正则表达式中表示要加个反斜杠‘\\n’,加个r就可以直接写\n

eval(plt[i].split(':')[1]

eval():将字符串str当成有效的表达式来求值并返回计算结果。

plt[i].split(':')[1]:split(':')将一个字符串按照正则表达式匹配结果进行分割,返回列表类型,用":"隔开。此式是将数组plt第i个字符串进行分割。

append()方法用于将传入的对象附加(添加)到现有列表中


  • 6
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值