鱼C论坛

 找回密码
 立即注册
查看: 728|回复: 3

[已解决]爬虫程序能运行但除了标题无输出。

[复制链接]
发表于 2019-6-5 09:23:50 | 显示全部楼层 |阅读模式

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

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

x
尝试爬取淘宝商品页面价格信息,代码如下:

  1. import requests
  2. import re

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

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

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

  28. def main():
  29.     goods = '书包'
  30.     depth = 2                                                                                     #希望能自动翻到第二页继续爬取
  31.     start_url = 'https://s.taobao.com/search?q=' + goods
  32.     infoList = []
  33.     for i in range(depth):
  34.         try:
  35.             url = start_url + '&s=' + str(44*i)
  36.             html = getHTMLText(url)
  37.             parsePage(infoList,html)
  38.         except:
  39.             continue
  40.     printGoodList(infoList)

  41. main()
复制代码






输出的结果为

  1. RESTART: C:/Users/Administrator/AppData/Local/Programs/Python/Python37-32/实战2淘宝爬取商品价格.py
  2. 序号          价格              商品名称   
复制代码
  



请问各位大佬神仙geek们,是哪里出了问题呀?
在线等。
最佳答案
2019-6-6 08:33:56
本帖最后由 chxchxkkk 于 2019-6-6 08:37 编辑

parsePage函数没有返回值,

infoList = []
    for i in range(depth):
        try:
            url = start_url + '&s=' + str(44*i)
            html = getHTMLText(url)
            parsePage(infoList,html)          ---》调用没有返回值的函数,所以什么也没有
        except:
            continue
    printGoodList(infoList)

def parsePage(ilt,html):
    try:
        plt = re.findall(r'"view_price"\:"[\d\.]*"',html)                                   #程序关键部分
        tlt = re.findall(r'"raw_title"\:".*?"',html)                                            #程序关键部分
        for i in range(len(plt)):
            price = eval(plt.split(':')[1])
            title = eval(tlt.split(':')[1])
            ilt.append([price,title])
       return ilt       ---->加上返回值
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-6-5 11:49:52 From FishC Mobile | 显示全部楼层
能爬淘宝的都是远古巨牛
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-5 11:59:40 From FishC Mobile | 显示全部楼层
头部信息一点都不模拟。也就是存活一次吧。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-6 08:33:56 | 显示全部楼层    本楼为最佳答案   
本帖最后由 chxchxkkk 于 2019-6-6 08:37 编辑

parsePage函数没有返回值,

infoList = []
    for i in range(depth):
        try:
            url = start_url + '&s=' + str(44*i)
            html = getHTMLText(url)
            parsePage(infoList,html)          ---》调用没有返回值的函数,所以什么也没有
        except:
            continue
    printGoodList(infoList)

def parsePage(ilt,html):
    try:
        plt = re.findall(r'"view_price"\:"[\d\.]*"',html)                                   #程序关键部分
        tlt = re.findall(r'"raw_title"\:".*?"',html)                                            #程序关键部分
        for i in range(len(plt)):
            price = eval(plt.split(':')[1])
            title = eval(tlt.split(':')[1])
            ilt.append([price,title])
       return ilt       ---->加上返回值
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-22 10:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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