大数据 爬虫Python AI Sqlpython爬虫入门

Python爬虫天猫店铺全部商品一记

2018-08-25  本文已影响5人  iHTCboy

1、前言

最近小姐姐工作需要,需要爬取天猫某店的全部商品,正好小哥学习了Python几个月,就答应上手试试!结果第一道题就难住了,天猫登陆需要账号密码和验证码!!!虽然知道可以通过模拟和Session操作,但是,始终是新手开车,还没有学习那么高深,感觉会走很多弯路!!另外,也想想,有没有什么更简单的方法???

不出意思,还真发现啦!天猫的手机版可以不用登陆,全部数据访问!!!就这样~

开始吧!

2、遇到的坑点

本文主要是在 利用Python爬虫爬取指定天猫店铺全店商品信息 - 晴空行 - 博客园 这个大哥的基础上,踩坑填坑,然后增加自己一些数据要求~

File "/Users/HTC/Documents/Programing/Python/WebCrawlerExample/WebCrawler/Tmall_demo.py", line 63, in get_products
    writer.writerows(products)
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/csv.py", line 158, in writerows
    return self.writer.writerows(map(self._dict_to_list, rowdicts))
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/csv.py", line 151, in _dict_to_list
    + ", ".join([repr(x) for x in wrong_fields]))
ValueError: dict contains fields not in fieldnames: 'titleUnderIconList'

writer.writerows 没有找到这个'titleUnderIconList'字段,这个字段应该是天猫的接口后来返回的数据,在代码里只能删除掉:

del product['titleUnderIconList']del product['titleUnderIconList']
File "/Users/HTC/Documents/Programing/Python/WebCrawlerExample/WebCrawler/Tmall_demo.py", line 65, in get_products
    writer.writerows(products)
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/csv.py", line 158, in writerows
    return self.writer.writerows(map(self._dict_to_list, rowdicts))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 26-27: ordinal not in range(128)

熟悉的人儿,看到python3与python2的区别,就知道,'ascii' codec can't encode 就是编码问题,问题就出来这里writer.writerows, python3处理、解析或转换和保存时,最好都指定一下使用 utf-8编码吧,特别是遇到中文的情况!

最后指定编码用utf-8:

with open(self.filename, 'a', encoding="utf-8", newline='') as f:
    writer = csv.DictWriter(f, fieldnames=title)
    writer.writerows(products)
035009803B0
图片下载错误 : http//img.alicdn.com/bao/uploaded/i4/821705368/TB1Sht8cfQs8KJjSZFEXXc9RpXa_!!0-item_pic.jpg Invalid URL '035009803B0': No schema supplied. Perhaps you meant http://035009803B0?
02100713003
图片下载错误 : http//img.alicdn.com/bao/uploaded/i1/821705368/TB1_OIkXQfb_uJkSmRyXXbWxVXa_!!0-item_pic.jpg Invalid URL '02100713003': No schema supplied. Perhaps you meant http://02100713003?
02800614023
图片下载错误 : http//img.alicdn.com/bao/uploaded/i3/821705368/TB1kKK6cInI8KJjSsziXXb8QpXa_!!0-item_pic.jpg Invalid URL '02800614023': No schema supplied. Perhaps you meant http://02800614023?

下图图片失败的提示,原因是天猫接口返回的商品数据如下:

{
item_id: 14292263734,
title: "XXXXXX",
img: "//img.alicdn.com/bao/uploaded/i2/821705368/TB1Us3Qcr_I8KJjy1XaXXbsxpXa_!!0-item_pic.jpg",
sold: "3",
quantity: 0,
totalSoldQuantity: 2937,
url: "//detail.m.tmall.com/item.htm?id=xxxxx",
price: "188.00",
titleUnderIconList: [ ]
},

不带协议名字!!!不知道是什么时候的历史留下的坑点吧!!!大厂也是有坑的!!

3、总结

具体的代码,可参考我github代码:

代码详细的解析还是参考这位大神的 利用Python爬虫爬取指定天猫店铺全店商品信息 - 晴空行 - 博客园,写的非常的详细!

整体来说,因为天猫的商品数据通过js来获取,所以比较容易获取到数据,而不用大量的爬取页面的商品,这个很赞!所以,爬虫这技术活,有很多方法,能找到好的方法,才是爬虫的最高境界啊!加油~

参考

注:本文首发于 iHTCboy's blog,如若转载,请注来源

上一篇 下一篇

猜你喜欢

热点阅读