python3爬取淘宝信息代码分析
作者:追心 时间:2021-05-03 04:59:11
# encoding:utf-8
import re # 使用正则 匹配想要的数据
import requests # 使用requests得到网页源码
这个函数是用来得到源码
# 得到主函数传入的链接
def getHtmlText(url):
try: # 异常处理
# 得到你传入的URL链接 设置超时时间3秒
r = requests.get(url, timeout=3)
# 判断它的http状态码
r.raise_for_status()
# 设置它的编码 encoding是设置它的头部编码 apparent_encoding是从返回网页中分析它的编码格式
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)
# 找到书包的地址
add = re.findall(r'\"item_loc\"\:\".*?\"', html)
# 找到书包的图片链接
img = re.findall(r'\"pic_url\"\:\".*?\"', html)
# 得到这个内容放入主函数中的列表
for i in range(len(plt)):
price = eval(plt[i].split(':')[1])
title = eval(tlt[i].split(':')[1])
address = eval(add[i].split(':')[1])
imgs = eval(img[i].split(':')[1])
ilt.append([price, title, address, imgs])
except: # 放生异常输出空字符串
print('')
# 得到主函数传入的列表
def printGoodsList(ilt):
# 每个列之间用tplt的放是隔开
tplt = '{:4}\t{:8}\t{:16}\t{:32}'
# 这个是整个的标题
print(tplt.format('序号', '价格', '商品名称','地址', '图片地址'))
count = 0 # 统计有多少的序号
for g in ilt:
count = count + 1 # 循环一遍加一
print(tplt.format(count, g[0], g[1], g[2]), g[3]) # 输出你得到的数据
# 定义主函数 main
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) # 得到url传入到你要得到url的函数中
parsePage(infoList, html) # 得到你的html源码 放入解析的网页中
except: # 发生异常跳过
continue
# 把列表中的数据放入解析的函数中
printGoodsList(infoList)
# 代码调试片段
if __name__ == '__main__':
main() # 调用主函数
来源:http://blog.csdn.net/qq_39248703/article/details/73921038
标签:python3,爬取淘宝信息
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python绘制字符画视频的示例代码
2023-11-09 16:21:46
![](https://img.aspxhome.com/file/2023/7/63957_0s.jpg)
如何使用共享连接减少空闲的连接数?
2010-05-16 15:15:00
MySQL与PHP的基础与应用专题之创建数据库表
2023-11-21 04:12:28
![](https://img.aspxhome.com/file/2023/2/110342_0s.png)
PyHacker编写指南引用Nmap模块实现端口扫描器
2023-07-15 14:15:40
![](https://img.aspxhome.com/file/2023/2/63602_0s.png)
各个版本IE合集下载,共存无冲突
2007-11-29 13:12:00
![](https://img.aspxhome.com/file/UploadPic/200711/29/20071129132016783s.png)
奇淫技巧之图片切割
2010-09-21 13:24:00
MySQL安全性指南 (1)(转)
2010-07-27 12:49:00
IE不支持border-spacing的解决办法
2009-04-28 13:11:00
MySql循环插入数据
2010-10-14 13:50:00
IE下绝对定位的元素不能响应鼠标的bug修正
2008-09-10 13:03:00
![](https://img.aspxhome.com/file/UploadPic/20089/10/20089101380482s.png)
解析:轻松掌握在 Mac OS X中安装MySQL
2009-01-14 11:51:00
浅淡BANNER设计
2010-09-25 18:37:00
![](https://img.aspxhome.com/file/UploadPic/20109/25/02-33s.jpg)
关于window.opener的用法
2008-02-23 11:03:00
SQL Server数据库超级管理员账号防护
2008-12-22 16:30:00
谈非线性任务流程的窗口打开方式
2008-08-28 12:47:00
![](https://img.aspxhome.com/file/UploadPic/20088/28/2008828125458515s.jpg)
asp如何连接MYSQL数据库?
2010-01-01 15:49:00
Python干货:分享Python绘制六种可视化图表
2023-07-10 10:01:03
![](https://img.aspxhome.com/file/2023/9/66369_0s.png)
Bootstrap编写导航栏和登陆框
2023-08-16 19:08:42
Python 中导入文本文件的示例代码
2021-04-18 04:53:01
JavaScript实现淘宝网图片的局部放大功能
2023-08-25 05:44:56
![](https://img.aspxhome.com/file/2023/0/56070_0s.jpg)