鱼C论坛

 找回密码
 立即注册
查看: 1837|回复: 2

萌新淘宝定向爬取商品信息遇到了问题

[复制链接]
发表于 2020-7-13 17:24:41 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import requests
  2. import re

  3. def getHTMLText(url):
  4. {"cookie":'',
  5.     "user-agent":"Mozilla/5.0"}
  6.     try:
  7.         r = requests.get(url, headers = kv, timeout = 30)
  8.         r.raise_for_status()
  9.         r.encoding = r.apparent_encoding
  10.         return r.text
  11.     except:
  12.         return ""

  13. def parsePage(ilt, html):
  14.     try:
  15.         plt = re.findall(r'"view_price"\:"[\d\.]*"', html)
  16.         tlt = re.findall(r'"raw_title"\:".*?"', html)
  17.         for i in range(len(plt)):
  18.             price = eval(plt[i].split(':')[1])
  19.             title = eval(tlt[i].split(':')[1])
  20.             ilt.append([price, title])
  21.     except:
  22.         print("")

  23. def printGoodsList(ilt):
  24.     tplt = "{:4}\t{:8}\t{:16}"
  25.     print(tplt.format("序号", "价格", "商品名"))
  26.     count = 0
  27.     for g in ilt:
  28.         count = count + 1
  29.         print(tplt.format(count, g[0], g[1]))

  30. def main():
  31.     goods = '书包'
  32.     depth = 2
  33.     start_url = 'http://s.taobao.com/search?q=' + goods
  34.     infoList = []
  35.     for i in range(depth):
  36.         try:
  37.             url = start_url + '&s=' + str(44*i)
  38.             html = getHTMLText(url)
  39.             parsePage(infoList, html)
  40.         except:
  41.             continue
  42.     printGoodsList(infoList)
  43.    
  44. main()
复制代码
以上是代码cookie我运行时候查了写里面了但是最后运行提示line38:Need type annotation for 'infoList' (hint: "infoList: List[<type>] = ...")
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-7-13 17:27:51 | 显示全部楼层
  1. def getHTMLText(url):
  2. kv = {"cookie":'',
  3.     "user-agent":"Mozilla/5.0"}
  4.     try:
  5.         r = requests.get(url, headers = kv, timeout = 30)
  6.         r.raise_for_status()
  7.         r.encoding = r.apparent_encoding
  8.         return r.text
  9.     except:
  10.         return ""
复制代码
不好意思刚才删cookie时候把kv = {}删了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-13 23:32:33 | 显示全部楼层

你代码发的有点乱,缩进也不对

  1. import requests
  2. import re


  3. def getHTMLText(url):
  4.     kv = {"cookie":'',
  5.     "user-agent":"Mozilla/5.0"}
  6.     try:
  7.         r = requests.get(url, headers = kv, timeout = 30)
  8.         r.raise_for_status()
  9.         r.encoding = r.apparent_encoding
  10.         return r.text
  11.     except:
  12.         return ""





  13. def parsePage(ilt, html):
  14.     try:
  15.         plt = re.findall(r'"view_price"\:"[\d\.]*"', html)
  16.         tlt = re.findall(r'"raw_title"\:".*?"', html)
  17.         for i in range(len(plt)):
  18.             price = eval(plt[i].split(':')[1])
  19.             title = eval(tlt[i].split(':')[1])
  20.             ilt.append([price, title])
  21.     except:
  22.         print("")


  23. def printGoodsList(ilt):
  24.     tplt = "{:4}\t{:8}\t{:16}"
  25.     print(tplt.format("序号", "价格", "商品名"))
  26.     count = 0
  27.     for g in ilt:
  28.         count = count + 1
  29.         print(tplt.format(count, g[0], g[1]))


  30. def main():
  31.     goods = '书包'
  32.     depth = 2
  33.     start_url = 'http://s.taobao.com/search?q=' + goods
  34.     infoList = []
  35.     for i in range(depth):
  36.         try:
  37.             url = start_url + '&s=' + str(44 * i)
  38.             html = getHTMLText(url)
  39.             parsePage(infoList, html)
  40.         except:
  41.             continue
  42.     printGoodsList(infoList)


  43. main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-23 13:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表