标签:返回 xpath get ext 项目 selector class test text
在Windows下安装scrapy框架。由于scrapy框架依赖较多,安装需要注意一下几点:
pip install pipywin32 pip install wheel #不用也可以 pip install Twisted pip install scrapy
scrapy startproject [项目名]
scrapy genspider [爬虫名] [域名]
import scrapy # from scrapy.selector.unified import SelectorList from scrapy1_test.items import Scrapy1TestItem class DuanziSpider(scrapy.Spider): name = ‘duanzi‘ allowed_domains = [‘ishuo.cn‘] start_urls = [‘http://ishuo.cn/‘] def parse(self, response): contents = [] content_lis = response.xpath("//div[@id=‘list‘]/ul/li") for li in content_lis: content = li.xpath(‘./div[1]/text()‘).get()#与etree的text()方法不同,这里拿到的是一个对象,需要get()才可以取出里面的内容 info = li.xpath(‘./div[2]/a/text()‘).get() item = Scrapy1TestItem(content=content,info=info) yield item #这种方式是单个item返回,也可以用一个列表集中返回 # contents.append(item) # return contents yield scrapy.Request
标签:返回 xpath get ext 项目 selector class test text
原文地址:https://www.cnblogs.com/gzwzx/p/12003106.html