※ 商品のリンクをクリックして何かを購入すると私に少額の報酬が入ることがあります【広告表示】 連打しようとしたのに、Referer忘れてて負けた。
ゴリゴリ
# -*- coding: utf-8 -*-
import re
import os.path
import urllib2, urllib
import cookielib
import time
def do_request(opener, url, post_data=None, additional_header=None):
req = urllib2.Request(url)
req.add_header('User-Agent', 'Mozilla/5.0 (Macintosh; U;
Intel Mac OS X; ja-jp) AppleWebKit/523.10.3 (KHTML, like Gecko)
Version/3.0.4 Safari/523.10')
req.add_header('Accept-Charset',
'utf-8,ISO-8859-1;q=0.7,*;q=0.7')
req.add_header('Accept', 'text/xml,application/xml,applicatio
n/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5')
req.add_header('Accept-Language', 'ja,en-us,en;q=0.5')
if additional_header is not None:
for k,v in additional_header.items():
req.add_header(k,v)
if post_data is not None:
return opener.open(req, urllib.urlencode(post_data))
return opener.open(req)
def main():
url = 'http://rhaco.lolipop.jp/study/'
cookie_file = 'cookie.txt'
csrf_value_re = re.compile(r'name="_onetimeticket"
value="([^"]+)"', re.M)
csrf_value = None
cookie = cookielib.LWPCookieJar()
if os.path.isfile(cookie_file):
cookie.load(cookie_file)
opener =
urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))
urllib2.install_opener(opener)
additional_header = {'Referer': url}
for i in range(0,30):
if i == 0:
post_data = None
else:
post_data = {'choice_submit': 'これでいい!',
'id': '26',
'_onetimeticket':
csrf_value}
response = do_request(opener, url,
post_data=post_data, additional_header=additional_header)
data = response.read()
if cookie is not None:
cookie.save(cookie_file, ignore_discard=True)
csrf_value = csrf_value_re.search(data).group(1)
time.sleep(1)
if __name__ == "__main__":
main()