python爬虫爬取淘宝商品比价(附淘宝反爬虫机制解决小办法)

时间:2022-11-05 17:52:39

因为评论有很多人说爬取不到,我强调几点

kv的格式应该是这样的:

  1. kv = {‘cookie':‘你复制的一长串cookie',‘user-agent':‘Mozilla/5.0'}

注意都应该用 ‘' ,然后还有个英文的 逗号,

kv写完要在后面的代码中添加

  1. r = requests.get(url, headers=kv,timeout=30)

自己得先登录自己的淘宝账号才有自己登陆的cookie呀,没登录cookie当然没用

以下原博

本人是python新手,目前在看中国大学MOOC的嵩天老师的爬虫课程,其中一个实例是讲如何爬取淘宝商品信息

以下是代码:

  1. import requests
  2. import re
  3.  
  4. def getHTMLText(url):
  5. try:
  6. r = requests.get(url, timeout=30)
  7. r.raise_for_status()
  8. r.encoding = r.apparent_encoding
  9. return r.text
  10. except:
  11. return ""
  12.  
  13. def parsePage(ilt, html):
  14. try:
  15. plt = re.findall(r'\"view_price\"\:\"[\d\.]*\"',html)
  16. tlt = re.findall(r'\"raw_title\"\:\".*?\"',html)
  17. for i in range(len(plt)):
  18. price = eval(plt[i].split(':')[1])
  19. title = eval(tlt[i].split(':')[1])
  20. ilt.append([price , title])
  21. except:
  22. print("")
  23.  
  24. def printGoodsList(ilt):
  25. tplt = "{:4}\t{:8}\t{:16}"
  26. print(tplt.format("序号", "价格", "商品名称"))
  27. count = 0
  28. for g in ilt:
  29. count = count + 1
  30. print(tplt.format(count, g[0], g[1]))
  31.  
  32. def main():
  33. goods = '书包'
  34. depth = 3
  35. start_url = 'https://s.taobao.com/search?q=' + goods
  36. infoList = []
  37. for i in range(depth):
  38. try:
  39. url = start_url + '&s=' + str(44*i)
  40. html = getHTMLText(url)
  41. parsePage(infoList, html)
  42. except:
  43. continue
  44. printGoodsList(infoList)
  45.  
  46. main()

但是我们运行的时候会发现这个程序没有出错,但是爬取不到,原因是淘宝实施了反爬虫机制,r.text 时是登录界面,我们如何绕过登录界面进行爬取呢?

首先我们需要先在浏览器中登录我们的个人淘宝,然后搜索以书包为例的商品,打开开发者模式(我使用的是chrome)或者按F12

python爬虫爬取淘宝商品比价(附淘宝反爬虫机制解决小办法)

这里我们可以看到我们当前的cookie和user-agent(一般是Mozilla/5.0)(注意:如果没有出现这几个name,点击浏览器刷新就会出现了)

然后在代码中增加我们的cookie和user-agent

python爬虫爬取淘宝商品比价(附淘宝反爬虫机制解决小办法)

然后运行

python爬虫爬取淘宝商品比价(附淘宝反爬虫机制解决小办法)

我只是个初学者,学的时候视频给不了答案,百度了很多,才发现这个小技巧,
有问题百度就完事了

完整代码

  1. import requests
  2. import re
  3.  
  4. def getHTMLText(url):
  5. kv = {'cookie':'t=5c749e8d453e7e3664735746f5eb5de8; cna=BrXNFDenbXUCAXggNKX9H1bo; thw=cn; tg=0; enc=5LMRHD8305w3oo8X0agYVFUDa7Ox%2F4RBf34oCWap48nRHy%2B%2B1RZCWZJ7ebn%2Fpy7vRNIL8xpS%2Ba0oNFXG5nSu8w%3D%3D; hng=CN%7Czh-CN%7CCNY%7C156; cookie2=10dbf1309bd9a2d5bc9cabe562965aee; _tb_token_=ee67e1a3ee0e5; alitrackid=www.taobao.com; swfstore=308730; v=0; unb=2448224718; sg=%E6%BB%A18d; _l_g_=Ug%3D%3D; skt=d53506c42f2db259; cookie1=BxUHGXuG%2B5Y4Iw7vZCvjLJ0zDvFL2Xy3mjxT%2FRPtFeA%3D; csg=4246b77f; uc3=vt3=F8dByEzfiHo1%2FUIk8VY%3D&id2=UUwU0bQkq1tYDQ%3D%3D&nk2=CN5OZuI3Xv2%2BlbVX&lg2=W5iHLLyFOGW7aA%3D%3D; existShop=MTU1MDU2MTUyMQ%3D%3D; tracknick=king%5Cu4E36%5Cu5C0F%5Cu4E30%5Cu6EE1; lgc=king%5Cu4E36%5Cu5C0F%5Cu4E30%5Cu6EE1; _cc_=VFC%2FuZ9ajQ%3D%3D; dnk=king%5Cu4E36%5Cu5C0F%5Cu4E30%5Cu6EE1; _nk_=king%5Cu4E36%5Cu5C0F%5Cu4E30%5Cu6EE1; cookie17=UUwU0bQkq1tYDQ%3D%3D; lastalitrackid=login.taobao.com; mt=ci=5_1; x=e%3D1%26p%3D*%26s%3D0%26c%3D0%26f%3D0%26g%3D0%26t%3D0%26__ll%3D-1%26_ato%3D0; uc1=cookie14=UoTZ5OXqJxXKdA%3D%3D&lng=zh_CN&cookie16=W5iHLLyFPlMGbLDwA%2BdvAGZqLg%3D%3D&existShop=false&cookie21=UIHiLt3xThH8t7YQouiW&tag=8&cookie15=UIHiLt3xD8xYTw%3D%3D&pas=0; JSESSIONID=F99B5E66516B99D5E7C9F431E402713F; l=bBNU0zKPvJ9oGfuLBOCNZuI8LN_OGIRYjuPRwCfMi_5B46JhzLQOllv3_FJ6Vj5RsK8B4z6vzNp9-etki; isg=BDg4VI5GkPAaMvx83RJGSPCNCeYKCZ0m9uCVOHKp6XNmjdh3GrU6uo2vQcWY5lQD; whl=-1%260%260%261550562673185',
  6. 'user-agent':'Mozilla/5.0'}
  7. try:
  8. r = requests.get(url, headers=kv,timeout=30)
  9. r.raise_for_status()
  10. r.encoding = r.apparent_encoding
  11. return r.text
  12. except:
  13. return ""
  14.  
  15. def parsePage(ilt, html):
  16. try:
  17. plt = re.findall(r'\"view_price\"\:\"[\d\.]*\"', html)
  18. tlt = re.findall(r'\"raw_title\"\:\".*?\"', html)
  19. for i in range(len(plt)):
  20. price = eval(plt[i].split(':')[1])
  21. title = eval(tlt[i].split(':')[1])
  22. ilt.append([price, title])
  23. except:
  24. print("")
  25.  
  26. def printGoodsList(ilt):
  27. tplt = "{:4}\t{:8}\t{:16}"
  28. print(tplt.format("序号", "价格", "商品名称"))
  29. count = 0
  30. for g in ilt:
  31. count = count + 1
  32. print(tplt.format(count, g[0], g[1]))
  33.  
  34. def main():
  35. goods = '书包'
  36. depth = 3
  37. start_url = 'https://s.taobao.com/search?q=' + goods
  38. infoList = []
  39. for i in range(depth):
  40. try:
  41. url = start_url + '&s=' + str(44 * i)
  42. html = getHTMLText(url)
  43. parsePage(infoList, html)
  44. except:
  45. continue
  46. printGoodsList(infoList)
  47.  
  48. main()

到此这篇关于python爬虫爬取淘宝商品比价(附淘宝反爬虫机制解决小办法)的文章就介绍到这了,更多相关python爬取淘宝商品内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

原文链接:https://blog.csdn.net/weixin_43173093/article/details/87716555