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

  • 时间:
  • 浏览:39

之前念爬与1些淘宝的数据,厥后发明需求登录,找了良多的材料,有个利用request的sessions减上cookie去登录的,cookie的获得正在登录后利用开辟者东西能够找到。不外那个登录后取得的网页的代码是静态的,获得静态网页借得另觅他法,1般需求的数据能够正在网页的源码中获得,可是您晓得的,有些静态减载的便没有是那末简朴了,并且我发明如许取得的源码中,有些念要获得的数据的格局是颠末窜改的,好比我要某个商品的详细链接,发明其实不能间接利用。 整体而行,那是1次失利的测验考试,不外却是领会到利用sessionscookies能够进到需求登录的网页,也算是1种体例吧。

记载1下失利的1次

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?q=ipad'
sessions.headers['User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36'
sessions.headers['cookie']='miid=8428352431475518963; hng=CN***********; cna=IzulExo***************; thw=cn; __guid=15467**********; enc=Ubrfp%2******************************************************; t=5********************1e0; tracknick=%5*********3; lgc=40***3; tg=0; x=e%3D1%26p%3D*%26s%3D0%26c%3D0%26f%3D0%26g%3D0%26t%3D0%26__ll%3D⑴%26_ato%3D0; cookie2=393e1f359e39e184059e8c87422bb5ce; v=0; _tb_token_=e7e035bee1ae8; _m_h5_tk=ebb49583b4434c3ff9f4bb277236a5d2_1541089384718; _m_h5_tk_enc=b0dd87431f8ade45b56bccb4982c0bf4; alitrackid=world.taobao.com; swfstore=29789; unb=3159140427; sg=374; _l_g_=Ug%3D%3D; skt=c9446f78d9091af3; cookie1=AHt5ehB%2FBw25k99NwMwTM4z3CWVA2J%2FVUVn4V3D2TMk%3D; csg=7b6476e0; uc3=vt3=F8dByRjNVxN9vRJQjTQ%3D&id2=UNGToApZ%2B2dYHA%3D%3D&nk2=sECE1uX4Wg%3D%3D&lg2=VFC%2FuZ9ayeYq2g%3D%3D; existShop=MTU0MTA4NzI2Ng%3D%3D; _cc_=Vq8l%2BKCLiw%3D%3D; dnk=%5Cu6C38%5Cu65E0%5Cu540D3; _nk_=%5Cu6C38%5Cu65E0%5Cu540D3; cookie17=UNGToApZ%2B2dYHA%'
for i in range(1):
  strs=str(i*44)
  urls=url+'&s='+strs
  html=sessions.get(urls).text
  doc=pq(html)
  doc=str(doc)
  os.chdir(r'G:\PS\PY')
  contentss=[]
  htmls=re.compile(r'p4pTags(.*?)"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⑻') as file:
    file.write(json.dumps(contentss,indent=2,ensure_ascii=False))
  time.sleep(2)#拜候距离

总结

以上所述是小编给各人引见的Python利用sessions摹拟登录淘宝,期望对各人有所帮忙,若是各人有任何疑问请给我留行,小编会实时复兴各人的。正在此也十分感激各人对剧本之家网站的撑持!
若是您以为本文对您有帮忙,欢送转载,烦请说明出处,开开!