简单爬取阿里巴巴商品信息

先码为敬
import requests
import re
def getHTMLText(url)://这里已经入门python爬虫的应该都知道了,这就是个爬取阿里商品全部信息的函数
    try:
        r=requests.get(url,timeout=30)
        r.raise_for_status()
        r.encoding=r.apparent_encoding
        return r.text
    except:
        return 'error'
def parsePage(ilt,html)://这个函数就是利用正则表达式从获取到的信息里面筛选需要的信息
    try:
        plt=re.findall(r'\"strPriceMoney\"\:\"[\d\.]*\"',html)//这里利用正则表达式进行信息匹配
        tlt=re.findall(r'\"title\"\:\".*?\"',html)
        for i in range(len(plt)):
            price=eval(plt[i].split(':')[1])
            title1=eval(tlt[i].split(':')[1])
            title=re.sub(r'<.*?>','',title1)\\这一段是利用正则表达式去除获取字符串里的标签信息
            ilt.append([price,title])
    except:
        print('error')
def printGoodsList(ilt)://这个函数就是将获取需要的信息进行打印输出
    tplt='{:4}\t{:8}\t{:16}'
    print(tplt.format('序号','价格','商品名称'))
    count=0
    for i in ilt:
        count+=1
        print(tplt.format(count,i[0],i[1]))

def main()://主函数进行调用控制其他函数功能
    goods='电脑'
    start_url='https://p4psearch.1688.com/p4p114/p4psearch/offer.htm?spm=a2609.11209760.it2i6j8a.6.50832de1NZAXIF&cosite=qqdaohang&keywords='+goods
    infoList=[]
    try:
        url=start_url
        html=getHTMLText(url)
        parsePage(infoList,html)
        printGoodsList(infoList)
    except:
        print('error')
main()
这段代码本来是在mooc上学习到的,但当时是用来爬取淘宝信息的,但现在淘宝要登录,我怕被封号^_^,就自己修改了一些内容从而来爬取阿里的商品信息
    

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值