在windows下 html
scrapy startproject myproject #myproject是你的项目名称python
cd 项目名称windows
scrapy genspider myspider 爬取域名 # myspider是你的爬虫名称 后跟爬取域名cookie
启动爬虫并发
scrapy crawl 爬虫名app
在setting.py 中配置dom
在你的myspider.py文件编写爬虫scrapy
import scrapy,re,requests from ..items import PerItem class LishiSpider(scrapy.Spider): name = 'myspider' #爬虫名
# allowed_domains = ['http://www.pearvideo.com/category_loading.jsp?reqType=5&categoryId=2&start=1'] start_urls = ['http://www.pearvideo.com/category_loading.jsp?reqType=5&categoryId=2&start=1'] #爬取的域名 def parse(self, response): # 标题 title = response.xpath('/html/body/li[@class="categoryem"]/div[@class="vervideo-bd"]/a//div[@class="vervideo-title"]/text()').extract() # 连接 t_url = response.xpath('/html/body/li[@class="categoryem"]/div[@class="vervideo-bd"]/a/@href').extract() # 时间 data = response.xpath('/html/body/li[@class="categoryem"]/div[@class="vervideo-bd"]/a//div[@class="cm-duration"]/text()').extract() #爬取的标题等需传到items.py里 for i in range(len(title)): item = PerItem() item['title'] = title[i] item['t_url'] = 'http://www.pearvideo.com/' + t_url[i] item['data'] = data[i] #yield item
print(item)
注意 :爬取的字段要跟 items.py里的一致jsp
import scrapy class PerItem(scrapy.Item): # define the fields for your item here like: # name = scrapy.Field() title = scrapy.Field() t_url = scrapy.Field() data = scrapy.Field() shi = scrapy.Field()
最后启动爬虫ide
scrapy crawl myspider