无法爬取淘宝商品页面

问题描述:无法爬取淘宝商品页面

案例如下:

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 = count + 1
        print(tplt.format(count,g[0],g[1]))

def main():
    goods = input("请输入要搜索的商品:")
    depth = input("请输入想要搜索的商品的页数:")
    depth = int(depth)
    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()

代码编译没有报错,以下是运行结果:
在这里插入图片描述
内容并未爬取到。

原因分析:因为淘宝的反爬虫机制导致爬取不了数据

查看淘宝的robots协议
https://www.taobao.com/robots.txt
在这里插入图片描述
分析:第一行 淘宝将爬虫定义为百度蜘蛛,第二行 它不允许爬虫爬取以’/'开头的路径

解决方案:添加headers内容

进入淘宝页面输入你想要搜索的内容
在这里插入图片描述
这边我搜索了笔记本电脑。

按F12进入控制台
在这里插入图片描述
依次点击Network,选择ALL,点击serch,找到上图勾选的serch,右击选择Copy,选择Copy as cURL(bash)
然后进去下方链接
https://curl.trillworks.com/

将内容复制到curl command框内
在这里插入图片描述
将Python requests内容中的headers内容复制到第一个函数getHTMLText中,并将
r = requests.get(url,timeout=30)修改为
r = requests.get(url,headers=headers,timeout=30)

import requests
import re

def getHTMLText(url):
    try:
        headers = {
            'authority': 's.taobao.com',
            'cache-control': 'max-age=0',
            'upgrade-insecure-requests': '1',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36',
            'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
            'sec-fetch-site': 'same-origin',
            'sec-fetch-mode': 'navigate',
            'sec-fetch-user': '?1',
            'sec-fetch-dest': 'document',
            'referer': '********',
            'accept-language': 'zh-CN,zh;q=0.9',
            'cookie': '******',}
             
        r = requests.get(url,headers=headers,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 = count + 1
        print(tplt.format(count,g[0],g[1]))

def main():
    goods = input("请输入要搜索的商品:")
    depth = input("请输入想要搜索的商品的页数:")
    depth = int(depth)
    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()

referer和cookie的内容太长,这边我就用*代替了,大家复制自己的headers就可以了

在这里插入图片描述
这样就可以正常爬取了。

上述案例为嵩天老师Python网络爬虫与信息提取课程中的案例。(本博客方法只为学习讨论,不做商业用途)

  • 39
    点赞
  • 102
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 23
    评论
评论 23
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

万家林

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值