-
Notifications
You must be signed in to change notification settings - Fork 1
/
scrapyspider.py
54 lines (40 loc) · 1.89 KB
/
scrapyspider.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# -*- coding: utf-8 -*-
# scrapy版本爬虫,只需要用scrapy genproject,然后复制到spider目录下即可,其他东西都没做修改
import scrapy
from scrapy import Request
from functools import reduce
from operator import add
import json,os
def transform(cookies):
cookie_dict = {}
cookies = cookies.replace(' ','')
list = cookies.split(';')
for i in list:
keys = i.split('=')[0]
values = i.split('=')[1]
cookie_dict[keys] = values
return cookie_dict
class GamejsonspiderSpider(scrapy.Spider):
name = 'gamejsonspider'
# accountId 也需要修改
accountId =
start_urls = 'http://lol.sw.game.qq.com/lol/api/?c=Battle&a=matchList&areaId=1&accountId=%s&queueId=70,72,73,75,76,78,96,98,100,300,310,313,317,318,325,400,420,430,450,460,600,610,940,950,960,980,990,420,440,470,83,800,810,820,830,840,850&r1=matchList'%accountId
Cookie = ""
def start_requests(self):
yield Request(url=self.start_urls,cookies=transform(self.Cookie),callback=self.parse)
def parse(self, response):
data = str(response.body,encoding='utf-8').strip("var matchList =")
jsondata = json.loads(data)
gameids = jsondata["msg"]["games"]
gameids = reduce(add,map(lambda x: [x["gameId"]],gameids))
for gameid in gameids:
url = "https://lol.sw.game.qq.com/lol/api/?c=Battle&a=combatGains&areaId=1&gameId=%s&r1=combatGains"%gameid
yield Request(url=url,cookies=transform(self.Cookie),callback=self.parsedetail,meta={'gameid':gameid})
def parsedetail(self,response):
if not os.path.exists("./battledetaildata"):
os.mkdir("./battledetaildata")
data = str(response.body).strip("var combatGains = ")
datafile = open("./battledetaildata/%s.json" % response.meta['gameid'], 'w')
print( response.meta['gameid'])
datafile.write(data)
datafile.close()