forked from james-see/python-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spider.py
50 lines (39 loc) · 1.74 KB
/
spider.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
#!/usr/bin/python3
# getting all links example crawling jamescampbell.us
# author: James Campbell
# Date Created: 2015 05 22
import sys # need this to pass arguments at the command line
from termcolor import colored # awesome color library for printing colored text in the terminal
import argparse, random
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors.lxmlhtml import LxmlLinkExtractor
from scrapy.item import Item, Field
# terminal arguments parser globals - do not change
parser = argparse.ArgumentParser()
parser.add_argument('-u', action='store', dest='url',
help='Domain to crawl')
parser.add_argument('-c', action='store_const', dest='constant_value',
const='value-to-store',
help='Store a constant value')
parser.add_argument('--version', action='version', version='%(prog)s 1.0')
results = parser.parse_args()
# setup the default search terms
domainer = 'jamescampbell.us' # default search term if none set is a random term from a dictionary list
if results.url != None: # if search terms set then change from default to that
domainer = results.url # set from argparse above in globals section
DOMAIN = domainer
URL = 'https://%s' % DOMAIN
class MyItem(Item):
url= Field()
class someSpider(CrawlSpider):
name = 'crawltest'
allowed_domains = ['jamescampbell.us']
start_urls = ['https://jamescampbell.us']
rules = (Rule(LxmlLinkExtractor(allow=()), callback='parse_obj', follow=True),)
def parse_obj(self,response):
item = MyItem()
item['url'] = []
for link in LxmlLinkExtractor(allow=(),deny = self.allowed_domains).extract_links(response):
item['url'].append(link.url)
print (link.url)
return item