python requests 自动管理 cookie 。 get后进行post发送数据---》最简单的刷票

  1. Request
  2. URL:
    http://musicman.migu.cn/activity/ccontent/voteWorks.do
  3. Request Method:
    POST
  4. Status Code:
    200 OK
  5. Request Headersview source
    1. Accept:
      */*
    2. Accept-Encoding:
      gzip,deflate,sdch
    3. Accept-Language:
      zh-CN,zh;q=0.8
    4. Connection:
      keep-alive
    5. Content-Length:
      12
    6. Content-Type:
      application/x-www-form-urlencoded
    7. Cookie:
      JSESSIONID=12FE4A10BEA8CAD8187CDB3269CEE8B8; vote_1868=1_20131020; vote_1850=5_20131020
    8. Host:
      musicman.migu.cn
    9. Origin:
      http://musicman.migu.cn
    10. Referer:
      http://musicman.migu.cn/activity/ccontent/queryWorksInfo.do?worksid=1850
    11. User-Agent:
      Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
    12. X-Requested-With:
      XMLHttpRequest
  6. Form Dataview sourceview URL encoded
    1. worksid:
      ###
  7. Response Headersview source
    1. Connection:
      close
    2. Content-Language:
      zh-CN
    3. Content-Length:
      17
    4. Content-Type:
      text/json;charset=utf-8
    5. Date:
      Sun, 20 Oct 2013 15:42:11 GMT
    6. P3P:
      CP='IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT'
    7. Server:
      Apache-Coyote/1.1

  8. 上面的是用 GoogleChrome 抓到的一份数据
    1. Cookie:
      JSESSIONID=12FE4A10BEA8CAD8187CDB3269CEE8B8; vote_1868=1_20131020; vote_1850=5_20131020
    这是cookie。。不过python的requests模块会自动管理cookie。 只需要先get那个网站一下就好了。

主要是抓到
  1. Form Dataview sourceview URL encoded
    1. worksid:
      ###

里面的数据。。
  1. Request
  2. URL:
    http://musicman.migu.cn/activity/ccontent/voteWorks.do
  3. Request Method:
    POST
  4. Status Code:
    200 OK
目标 URL 跟方法是post。。然后没了

import requests,time
i = 0
data={"worksid":"###"}
s = requests.session()
s.get('http://musicman.migu.cn/activity/ccontent/queryWorksInfo.do?worksid=1850')
while i < 10 :
    i=i+1
    z = s.post('http://musicman.migu.cn/activity/ccontent/voteWorks.do',data)
    print z.text


然后就没了。。不过只是试验下。。


下面用urllib2,跟urllib模块重写了下。。
import urllib,cookielib
import urllib2
import json

cj=cookielib.CookieJar()
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)

for ip in range(23,24):
    opener.open('http://musicman.migu.cn/activity/ccontent/queryWorksInfo.do?worksid=####')

    req=urllib2.Request('http://musicman.migu.cn/activity/ccontent/voteWorks.do')
    data={"worksid":"####"}
    ipAddress="192.168.4."+str(ip)
    #headers = {'X-Forwarded-For':ipAddress,'User-Agent':'Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20100101 Firefox/11.0'}
    req.add_header('Content-Type','application/x-www-form-urlencoded')
    req.add_header('X-Forwarded-For',ipAddress)
    req.add_header('User-Agent','Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20100101 Firefox/11.0')
    for i in range(10):
        k=opener.open("http://musicman.migu.cn/activity/ccontent/voteWorks.do",urllib.urlencode(data))
        print k.read()


你可能感兴趣的:(python,抓取数据)