鱼C论坛

 找回密码
 立即注册
查看: 750|回复: 5

求助爬淘宝出现返回空数据

[复制链接]
发表于 2019-3-23 15:15:39 | 显示全部楼层 |阅读模式

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

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

x
  1. def getHTML(url):
  2.     try:
  3.         r = requests.get(url,timeout = 30)
  4.         r.raise_for_status()
  5.         r.encoding = r.apparent_enconding
  6.         return r.text
  7.     except:
  8.         return ''
  9.    

  10. def paresHTML(ilt,html):
  11.     try:
  12.         plt = re.findall(r'"view_price":"[\d\.]*"',html)   #正则:搜索所有'view_price'字符串开头并包含字符串后面的数字信息(价格),返回列表
  13.         tlt = re.findall(r'"raw_tltle":".*?"',html)
  14.         for i in range(len(plt)):
  15.             price = eval(plt[i].split(':')[1])   #eval去掉引号
  16.             title = eval(tlt[i].spilt(':')[1])                     
  17.             ilt.append([prince,title])
  18.     except:
  19.         print('')
  20.         
  21. def printGoods(ilt):
  22.     tplt = '{:^6}\t{:^10}\t{:^16}'
  23.     print(tplt.format('序号','价格','名字'))
  24.     count = 0
  25.     for g in ilt:
  26.         count += 1
  27.         print(tplt.format(count,u[0],u[1]))
  28.         
  29. def main():
  30.     goods = '神舟笔记本'
  31.     start_url = 'https://s.taobao.com/search?q=' + goods
  32.     depth = 2
  33.     infoList = []
  34.     for i in range(depth):
  35.         try:
  36.             url = start_url + '&s=' + str(44*i)
  37.             html = getHTML(url)
  38.             parseHTML(infoList,html)
  39.         except:
  40.             continue
  41.     printGoods(infoList)

  42. main()
复制代码



还是没有搞懂  为什么返回的是空
而且  我在淘宝页面  f12  查看的商品价格的前缀有trace-price和view_price   不知道选哪个   
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-3-23 15:20:00 | 显示全部楼层
运行之后  结果为空   我觉得应该是  爬取网站内容的部分 parseHTML 部分出现了问题;可能是正则表达式搜索r.text内容的时候没有搜索到相匹配的信息


我在淘宝页面  搜索到的关键字如下图
C:\Users\Administrator\Desktop\111.jpg
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-23 15:20:31 | 显示全部楼层
def getHTML(url):
    try:
        r = requests.get(url,timeout = 30)
        r.raise_for_status()
        r.encoding = r.apparent_enconding
        return r.text
    except:
        return ''
   

def paresHTML(ilt,html):
    try:
        plt = re.findall(r'"view_price":"[\d\.]*"',html)   #正则:搜索所有'view_price'字符串开头并包含字符串后面的数字信息(价格),返回列表
        tlt = re.findall(r'"raw_tltle":".*?"',html)
        for i in range(len(plt)):
            price = eval(plt[i].split(':')[1])   #eval去掉引号
            title = eval(tlt[i].spilt(':')[1])                     
            ilt.append([prince,title])
    except:
        print('')
        
def printGoods(ilt):
    tplt = '{:^6}\t{:^10}\t{:^16}'
    print(tplt.format('序号','价格','名字'))
    count = 0
    for g in ilt:
        count += 1
        print(tplt.format(count,u[0],u[1]))
        
def main():
    goods = '神舟笔记本'
    start_url = 'https://s.taobao.com/search?q=' + goods
    depth = 2
    infoList = []
    for i in range(depth):
        try:
            url = start_url + '&s=' + str(44*i)
            html = getHTML(url)
            parseHTML(infoList,html)
        except:
            continue
    printGoods(infoList)

main()

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-23 16:59:51 | 显示全部楼层
说明一下具体思路
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-23 17:12:31 | 显示全部楼层
苏绛雪 发表于 2019-3-23 16:59
说明一下具体思路

是不是因为  如果没有登录淘宝就没有办法搜索的原因啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-23 21:29:36 | 显示全部楼层
有没有大佬帮忙看下是不是正则表达式的问题啊。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-14 17:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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