首页 Scrapy爬虫系列2:爬取豆瓣书籍排行榜
文章
取消

Scrapy爬虫系列2:爬取豆瓣书籍排行榜

爬取网站 http://book.douban.com/

起始地址:https://book.douban.com/subject/25862578/

要将代码从使用MySQL数据库改为使用本地的SQLite数据库

• MySQL使用%s作为占位符,而SQLite使用?作为占位符。

self.dbpool = adbapi.ConnectionPool(‘sqlite3’, database=’doubanbooks.db’, check_same_thread=False)

使用选择器

1
2
3
4
5
6
sel = Selector(response)# use response.xpath
item['name']  = sel.xpath('//*[@id="wrapper"]/h1/span/text()').extract()
year = sel.xpath(u'//*[@id="info"]//span[@class="pl" and contains(./text(), "出版年:")]/following::text()[1]').extract()
item['year']  = year[0].strip()
item['score'] = sel.xpath('//*[@id="interest_sectl"]/div/div[2]/strong/text()').extract()
item['vote']  = sel.xpath('//*[@id="interest_sectl"]/div/div[2]/div/div[2]/span/a/span/text()').re(r'\d+')
1
2
3
4
 rules = [
        Rule(LinkExtractor(allow=(r'https://book.douban.com/subject/\d+'), restrict_xpaths='//*[@id="db-rec-section"]/div'),
             callback="parse_item", follow=True),
    ]

欢迎评论交流

本文由作者按照 CC BY 4.0 进行授权