欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python使用sessions模擬登錄淘寶的方式

 更新時間:2019年08月16日 14:06:17   作者:吳子夜  
這篇文章主要介紹了python使用sessions模擬登錄淘寶的方式,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值 ,需要的朋友可以參考下

之前想爬取一些淘寶的數(shù)據(jù),后來發(fā)現(xiàn)需要登錄,找了很多的資料,有個使用request的sessions加上cookie來登錄的,cookie的獲取在登錄后使用開發(fā)者工具可以找到。不過這個登錄后獲得的網(wǎng)頁的代碼是靜態(tài)的,獲取動態(tài)網(wǎng)頁還得另尋他法,一般需要的數(shù)據(jù)可以在網(wǎng)頁的源碼中得到,但是你知道的,有些動態(tài)加載的就不是那么簡單了,而且我發(fā)現(xiàn)這樣獲得的源碼中,有些想要獲取的數(shù)據(jù)的格式是經(jīng)過改動的,比如我要某個商品的具體鏈接,發(fā)現(xiàn)并不能直接使用。 總體而言,這是一次失敗的嘗試,不過倒是了解到使用sessionscookies可以進(jìn)到需要登錄的網(wǎng)頁,也算是一種方式吧。

記錄一下失敗的一次

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-1%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))
  #提取信息的正則表達(dá)式
  raw_title=r'"raw_title":"(.*?)"'
  view_price= r'"view_price":"(.*?)"' #價格
  view_fee=r'"view_fee":"(.*?)"'   #折扣
  item_loc = r'"item_loc":"(.*?)"' #地區(qū)
  view_sales = r'"view_sales":"(.*?)"' #付款人數(shù)
  comment_count = r'"comment_count":"(.*?)"' #評論數(shù)
  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)#訪問間隔

總結(jié)

以上所述是小編給大家介紹的python使用sessions模擬登錄淘寶,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!

相關(guān)文章

最新評論