# 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"htmlcode"># 代码调试片段 if __name__ == '__main__': main() # 调用主函数以上就是经过小编测试过的用python3爬取淘宝信息的代码,大家测试后如果还有任何不明白的地方可以在下方的留言区讨论。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月20日
2024年09月20日
- 陈奕迅.2013-Easons.Life演唱会2CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 许美静.1995-遗憾(新马版)【上华】【WAV+CUE】
- 《叶倩文 歌声情缘》[WAV+CUE][410MB]
- 《张国荣 首首动听经典不容错过 追忆的风 2CD》[WAV+CUE][870MB]
- 《腾格尔 容中尔甲 亚东 高原三星 男人篇 3CD》[WAV/分轨][1GB]
- 命运圣契公测实测可用兑换码大全 命运圣契最新兑换码分享
- 黑神话悟空上品疾蝠精魄获取方法一览|上品疾蝠精魄收集攻略
- 《七龙珠电光炸裂!ZERO》GT角色预告片曝光,15位新角色登场
- [ABC]安娜-胆麦发烧女声[6N纯银镀膜][2016[低速原抓WAV+CUE]
- NewViennaOctetViennaWindSoloists-TheDeccaRecordings(2024)18CD[24-48][FLAC]-7
- 雨果唱片-三角琴《俄罗斯民间音乐系列-梁祝(中国名曲)》wav
- 《塞尔达传说:智慧的再现》新实机!无之世界介绍
- 任天堂公布《塞尔达传说》系列时间线:野炊与王泪独立在外
- 任天堂更新网络投稿规范:打击影响他人游戏体验行为
- 冼佩瑾.2024-女儿Daughter(EP)【杰威尔】【FLAC分轨】