python使用sessions模拟登录淘宝的方式

(编辑:jimmy 日期: 2024/9/26 浏览:2)

之前想爬取一些淘宝的数据,后来发现需要登录,找了很多的资料,有个使用request的sessions加上cookie来登录的,cookie的获取在登录后使用开发者工具可以找到。不过这个登录后获得的网页的代码是静态的,获取动态网页还得另寻他法,一般需要的数据可以在网页的源码中得到,但是你知道的,有些动态加载的就不是那么简单了,而且我发现这样获得的源码中,有些想要获取的数据的格式是经过改动的,比如我要某个商品的具体链接,发现并不能直接使用。 总体而言,这是一次失败的尝试,不过倒是了解到使用sessionscookies可以进到需要登录的网页,也算是一种方式吧。

记录一下失败的一次

import requests
import os
import json
from pyquery import PyQuery as pq
import re
import time
sessions = requests.session()
url = 'https://s.taobao.com/search"risk"')   
  garbage=re.compile(r'itemlist(.*"risk"')
  gb=garbage.findall(doc,re.S|re.M)
  finhtml=htmls.findall(doc,re.S|re.M)
  finhtml=finhtml+gb
  print(len(finhtml))
  #提取信息的正则表达式
  raw_title=r'"raw_title":"(.*"'
  view_price= r'"view_price":"(.*"' #价格
  view_fee=r'"view_fee":"(.*"'   #折扣
  item_loc = r'"item_loc":"(.*"' #地区
  view_sales = r'"view_sales":"(.*"' #付款人数
  comment_count = r'"comment_count":"(.*"' #评论数
  detail_url=r'"detail_url":"(.*"'   #url    
  for html in finhtml:
    rtitle=re.findall(raw_title,html)
    price=re.findall(view_price,html)
    fee=re.findall(view_fee,html)
    loc=re.findall(item_loc,html)
    sales= re.findall(view_sales,html)
    comment=re.findall(comment_count,html)
    deurl=re.findall(detail_url,html)
    for rt,p,f,l,s,c,u in zip(rtitle,price,fee,loc,sales,comment,deurl):
      contentss.append({"raw_title":rt,"view_price":p,"view_fee":f,"item_loc":l,"view_sales":s,"comment_count":c,"detail_url":u})  
  with open('ipad.json','a',encoding='utf-8') as file:
    file.write(json.dumps(contentss,indent=2,ensure_ascii=False))
  time.sleep(2)#访问间隔

总结

以上所述是小编给大家介绍的python使用sessions模拟登录淘宝,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。