python爬虫爬取淘宝商品信息

yipeiwu_com6年前Python爬虫

本文实例为大家分享了python爬取淘宝商品的具体代码,供大家参考,具体内容如下

import requests as req 
import re 
 
def getHTMLText(url): 
 try: 
 r = req.get(url, timeout=30) 
 r.raise_for_status() 
 r.encoding = r.apparent_encoding 
 return r.text 
 except: 
 return "" 
 
def parasePage(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 j in ilt: 
 count = count + 1 
 print(tplt.format(count, j[0], j[1])) 
 
def main(): 
 goods = "python爬虫" 
 depth = 3 
 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) 
  parasePage(infoList, html) 
 except: 
  continue 
 printGoodsList(infoList) 
 
main() 

效果图:

更多内容请参考专题《python爬取功能汇总》进行学习。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

零基础写python爬虫之爬虫的定义及URL构成

一、网络爬虫的定义网络爬虫,即Web Spider,是一个很形象的名字。把互联网比喻成一个蜘蛛网,那么Spider就是在网上爬来爬去的蜘蛛。网络蜘蛛是通过网页的链接地址来寻找网页的。从网站...

详解爬虫被封的问题

详解爬虫被封的问题

如果你在爬虫过程中有遇到“您的请求太过频繁,请稍后再试”,或者说代码完全正确,可是爬虫过程中突然就访问不了,那么恭喜你,你的爬虫被对方识破了,轻则给予友好提示警告,严重的可能会对你的ip...

Python3网络爬虫中的requests高级用法详解

Python3网络爬虫中的requests高级用法详解

本节我们再来了解下 Requests 的一些高级用法,如文件上传,代理设置,Cookies 设置等等。 1. 文件上传 我们知道 Reqeuests 可以模拟提交一些数据,假如有的网站需...

itchat和matplotlib的结合使用爬取微信信息的实例

itchat和matplotlib的结合使用爬取微信信息的实例

前几天无意中看到了一片文章,《用 Python 爬了爬自己的微信朋友(实例讲解)》,这篇文章写的是使用python中的itchat爬取微信中朋友的信息,其中信息包括,昵称、性别、地理位置...

如何准确判断请求是搜索引擎爬虫(蜘蛛)发出的请求

如何准确判断请求是搜索引擎爬虫(蜘蛛)发出的请求

网站经常会被各种爬虫光顾,有的是搜索引擎爬虫,有的不是,通常情况下这些爬虫都有UserAgent,而我们知道UserAgent是可以伪装的,UserAgent的本质是Http请求头中的一...