|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 学程序的古代人 于 2018-12-24 21:20 编辑
import scrapy
class AuthorSpider(scrapy.Spider):
name = 'author'
start_urls = ['http://quotes.toscrape.com/']
def parse(self, response):
# follow links to author pages
for href in response.css('.author + a::attr(href)'):
yield response.follow(href, self.parse_author)
# follow pagination links
for href in response.css('li.next a::attr(href)'):
yield response.follow(href, self.parse)
def parse_author(self, response):
def extract_with_css(query): #这里不明白
return response.css(query).extract_first().strip() #query代表了何种规则吗?怎么定义的呢?
yield {
'name': extract_with_css('h3.author-title::text'),
'birthdate': extract_with_css('.author-born-date::text'),
'bio': extract_with_css('.author-description::text'),
定义extract_with_css(query): 时,query 应该如何理解?为什么这样就可以提取出来
response.css(query).extract_first().strip() ---->这个调用的是CSS选择器,scrapy支持xpath、css、正则的提取元素的方法,你只要调用就可以了,而query传入的是CSS选择器的语法
参数不需要定义,只要你调用该函数的时候传入数值就可以了
- yield {
- 'name': extract_with_css('h3.author-title::text'),
- 'birthdate': extract_with_css('.author-born-date::text'),
- 'bio': extract_with_css('.author-description::text')}
- 你在这里调用时就要传入css选择器的语法
复制代码
|
|