python编程-26:实例2-淘宝商品信息定向爬虫

举报
彭世瑜 发表于 2021/08/15 00:48:39 2021/08/15
【摘要】 源码: #CrowTaobaoPrice.py import requests import re   def getHTMLText(url):     try:     &nbsp...

python编程-26:实例2-淘宝商品信息定向爬虫

源码:
  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()

文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。

原文链接:pengshiyu.blog.csdn.net/article/details/81049364

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。