-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathitemcrawl.py
55 lines (42 loc) · 1.93 KB
/
itemcrawl.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
52
53
54
55
import re
import scrapy
from models import Item
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from pymongo import MongoClient
engine = create_engine('postgresql://dota:dota@localhost/dota', echo=True)
Session = sessionmaker(bind=engine)
session = Session()
client = MongoClient()
db = client.dota
items = db.items
class ItemSpider(scrapy.Spider):
name = 'itemspider'
def start_requests(self):
urls = ['http://dota.wikia.com/wiki/Category:Items_(DOTA2)']
for url in urls:
yield scrapy.Request(url=url, callback=self.parse)
def parse(self, response):
for item in response.xpath('//body//div[@class="mw-content-ltr"]//li/a/@href'):
yield response.follow('http://dota.wikia.com' + str(item.extract()), self.parse)
# yield {'heroe': 'http://dota.wikia.com' + str(hero.extract())}
item_name = response.url.split('/')[-1]
item_desc = response.xpath('//body//table[@class="infobox"]//td[@colspan="2"]//i/text()').extract()[0]
item_effect = response.xpath('//body//table[@class="infobox"]//tr[4]//td[last()]/text()').extract()[0]
item_cost = response.xpath('//body//table[@class="infobox"]//tr[6]//td[last()]/text()').extract()[0]
item = Item()
item.name = item_name
item.description = re.sub('<.*?>', '', item_desc).replace('\n', '')
item.effects = re.sub('<.*?>', '', item_effect).replace('\n', '')
item.cost = int(re.sub('<.*?>', '', item_cost).replace('\n', ''))
mongo_item = {
'name': item_name,
'description': re.sub('<.*?>', '', item_desc).replace('\n', ''),
'effects': re.sub('<.*?>', '', item_effect).replace('\n', ''),
'cost': int(re.sub('<.*?>', '', item_cost).replace('\n', '')),
}
items.insert_one(mongo_item)
session.add(item)
session.commit()
self.log(item_effect)
session.close()