鱼C论坛

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

有大神看看这段代码,淘宝网页数据爬取不成功

[复制链接]
发表于 2019-10-27 19:45:12 | 显示全部楼层 |阅读模式

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

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

x
错误不知道在哪里?来位大神指教一下,谢谢!

代码如下:


# 淘宝信息爬取

import requests
import re

def getHTMLText(url):
    try:
        r = requests.get(url,timeout=30)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except:
        return " "
   
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[i].split(':')[1])
            title = eval(tlt[i].split(':')[1])
            ilt.append([price,title])
    except:
        print("")
                     
def printGoodsList(ilt):
    tplt = "{:4}\t{:8}\t{:16}"
    print(tplt.format("序号","价格","商品名称"))
    count = 0
    for g in ilt:
        count += 1
        print(tplt.format(count,g[0],g[1]))
      
def main():
    goods = "牛排"
    depth = 2
    start_url = 'https://s.taobao.com/search?q=' + goods
    infoList = []
    for i in range(depth):
        try:
            url = start_url + '&s=' + str(44*i)
            html = getHTMLText(url)
            parsePage(infoList,html)
            
        except:
            continue

    printGoodsList(infoList)

main()
   
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-10-27 21:42:46 | 显示全部楼层
我也刚学到这,你爬出来的html 应该是登陆那个界面吧,淘宝不像之前了,现在搜索也需要账号密码,我也不知道怎么爬来着
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-10-28 07:10:57 | 显示全部楼层
foreverc8 发表于 2019-10-27 21:42
我也刚学到这,你爬出来的html 应该是登陆那个界面吧,淘宝不像之前了,现在搜索也需要账号密码,我也不知 ...

我只有序号、价格、商品名称  其他数据都么有
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-20 08:10

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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