在提取器过滤数据这个地方被坑了很久,确实有点坑,有点难以理解,多注意下就可以了。

from multiprocessing import allow_connection_pickling
from scrapy.spiders import Spider
from ..items import Cnblogshaha01Item

class cnblogSpider(Spider):
    name="cnblogsHAHA01" #定义爬虫名称
    allow_connection_pickling=['www.cnblogs.com'] #定义爬虫域
    start_urls = ['https://www.cnblogs.com/huaan011'] #定义开始爬虫链接

    def parse(self,response) :
        item_nodes = response.css(".post")
        for item_node in item_nodes:
            item = Cnblogshaha01Item()
            #参考文档: https://www.w3cschool.cn/scrapy2_3/scrapy2_3-ms5x3fng.html
            # item['name']= item_node.xpath('/span/text()').extract_first().strip() #这个表示从response中开始提取所有的满足搜索条件的值
            # item['name']= item_node.xpath('//span/text()').extract_first().strip() #这个表示从response中开始提取所有的满足搜索条件的值
            #item['name']= item_node.xpath('h2/a/span/text()').extract_first().strip() #这个表示从当前的选择器中开始提取所有的满足搜索条件的值
            item['name']= item_node.xpath('.//span/text()').extract_first().strip() #这个表示从当前的选择器中开始提取所有的满足搜索条件的值
            #item['name']= item_node.xpath('./h2/a/span/text()').extract_first().strip() #这个表示从当前的选择器中开始提取所有的满足搜索条件的值
       #item['name']= item_node.css('span::text').getall()[0].strip(); #这个表示从当前的选择器中开始提取所有满足搜索条件的值
print(item['name']) yield item

参考文档: 

https://www.w3cschool.cn/scrapy2_3/scrapy2_3-ms5x3fng.html
https://www.runoob.com/xpath/xpath-syntax.html
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。