-
Notifications
You must be signed in to change notification settings - Fork 545
Expand file tree
/
Copy path5-2-scrapy.py
More file actions
24 lines (18 loc) · 757 Bytes
/
5-2-scrapy.py
File metadata and controls
24 lines (18 loc) · 757 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import scrapy
class MofanSpider(scrapy.Spider):
name = "mofan"
start_urls = [
'https://mofanpy.com/',
]
# unseen = set()
# seen = set() # we don't need these two as scrapy will deal with them automatically
def parse(self, response):
yield { # return some results
'title': response.css('h1::text').extract_first(default='Missing').strip().replace('"', ""),
'url': response.url,
}
urls = response.css('a::attr(href)').re(r'^/.+?/$') # find all sub urls
for url in urls:
yield response.follow(url, callback=self.parse) # it will filter duplication automatically
# lastly, run this in terminal
# scrapy runspider 5-2-scrapy.py -o res.json