|
一、安装及常用命令介绍
1. 安装
Linux:pip3 install scrapy
Windows:
a. pip3 install wheel
b. 下载twisted http://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted
c. shift右击进入下载目录,执行 pip3 install typed_ast-1.4.0-cp36-cp36m-win32.whl
d. pip3 install pywin32
e. pip3 install scrapy
2.scrapy基本命令行
- (1)创建一个新的项目
- scrapy startproject ProjectName
- (2)生成爬虫
- scrapy genspider +SpiderName+website
- (3)运行(crawl) # -o output
- scrapy crawl +SpiderName
- scrapy crawl SpiderName -o file.json
- scrapy crawl SpiderName-o file.csv
- (4)check检查错误
- scrapy check
- (5)list返回项目所有spider名称
- scrapy list
- (6)view 存储、打开网页
- scrapy view https://www.baidu.com
- (7)scrapy shell,进入终端
- scrapy shell https://www.baidu.com
- (8)scrapy runspider
- scrapy runspider zufang_spider.py
复制代码
二、简单实例
以麦田租房信息爬取为例,网站http://bj.maitian.cn/zfall/PG1
1.创建项目
- scrapy startproject houseinfo
复制代码
生成项目结构:
scrapy.cfg 项目的主配置信息。(真正爬虫相关的配置信息在settings.py文件中)
items.py 设置数据存储模板,用于结构化数据,如:Django的Model
pipelines 数据持久化处理
settings.py 配置文件
spiders 爬虫目录
2.创建爬虫应用程序
- cd houseinfo
- scrapy genspider maitian maitian.com
复制代码
然后就可以在spiders目录下看到我们的爬虫主程序
3.编写爬虫文件
步骤2执行完毕后,会在项目的spiders中生成一个应用名的py爬虫文件,文件源码如下:
- # -*- coding: utf-8 -*-
- import scrapy
- class MaitianSpider(scrapy.Spider):
- name = 'maitian' # 应用名称
- allowed_domains = ['maitian.com'] #一般注释掉,允许爬取的域名(如果遇到非该域名的url则爬取不到数据)
- start_urls = ['http://maitian.com/'] #起始爬取的url列表,该列表中存在的url,都会被parse进行请求的发送
-
- #解析函数
- def parse(self, response):
- pass
复制代码
我们可以在此基础上,根据需求进行编写
- # -*- coding: utf-8 -*-
- import scrapy
- class MaitianSpider(scrapy.Spider):
- name = 'maitian'
- start_urls = ['http://bj.maitian.cn/zfall/PG100']
- #解析函数
- def parse(self, response):
- li_list = response.xpath('//div[@class="list_wrap"]/ul/li')
- results = []
- for li in li_list:
- title = li.xpath('./div[2]/h1/a/text()').extract_first().strip()
- price = li.xpath('./div[2]/div/ol/strong/span/text()').extract_first().strip()
- square = li.xpath('./div[2]/p[1]/span[1]/text()').extract_first().replace('㎡','') # 将面积的单位去掉
- area = li.xpath('./div[2]/p[2]/span/text()[2]').extract_first().strip().split('\xa0')[0] # 以空格分隔
- adress = li.xpath('./div[2]/p[2]/span/text()[2]').extract_first().strip().split('\xa0')[2]
- dict = {
- "标题":title,
- "月租金":price,
- "面积":square,
- "区域":area,
- "地址":adress
- }
- results.append(dict)
- print(title,price,square,area,adress)
- return results
复制代码
须知:
xpath为scrapy中的解析方式
xpath函数返回的为列表,列表中存放的数据为Selector类型数据。解析到的内容被封装在Selector对象中,需要调用extract()函数将解析的内容从Selector中取出。
如果可以保证xpath返回的列表中只有一个列表元素,则可以使用extract_first(), 否则必须使用extract()
两者等同,都是将列表中的内容提取出来
title = li.xpath('./div[2]/h1/a/text()').extract_first().strip()
title = li.xpath('./div[2]/h1/a/text()')[0].extract().strip()
4. 设置修改settings.py配置文件相关配置:
- #伪装请求载体身份
- USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
- #可以忽略或者不遵守robots协议
- ROBOTSTXT_OBEY = False
复制代码
5.执行爬虫程序:scrapy crawl maitain
爬取全站数据,也就是全部页码数据。本例中,总共100页,观察页面之间的共性,构造通用url
方式一:通过占位符,构造通用url
- import scrapy
- class MaitianSpider(scrapy.Spider):
- name = 'maitian'
- start_urls = ['http://bj.maitian.cn/zfall/PG{}'.format(page) for page in range(1,4)] #注意写法
- #解析函数
- def parse(self, response):
- li_list = response.xpath('//div[@class="list_wrap"]/ul/li')
- results = []
- for li in li_list:
- title = li.xpath('./div[2]/h1/a/text()').extract_first().strip()
- price = li.xpath('./div[2]/div/ol/strong/span/text()').extract_first().strip()
- square = li.xpath('./div[2]/p[1]/span[1]/text()').extract_first().replace('㎡','')
- # 也可以通过正则匹配提取出来
- area = li.xpath('./div[2]/p[2]/span/text()[2]')..re(r'昌平|朝阳|东城|大兴|丰台|海淀|石景山|顺义|通州|西城')[0]
- adress = li.xpath('./div[2]/p[2]/span/text()[2]').extract_first().strip().split('\xa0')[2]
- dict = {
- "标题":title,
- "月租金":price,
- "面积":square,
- "区域":area,
- "地址":adress
- }
- results.append(dict)
- return results
复制代码
如果碰到一个表达式不能包含所有情况的项目,解决方式是先分别写表达式,最后通过列表相加,将所有url合并成一个url列表,例如
- start_urls = ['http://www.guokr.com/ask/hottest/?page={}'.format(n) for n in range(1, 8)] + [
- 'http://www.guokr.com/ask/highlight/?page={}'.format(m) for m in range(1, 101)]
复制代码
方式二:通过重写start_requests方法,获取所有的起始url。(不用写start_urls)
三、数据持久化存储
基于终端指令的持久化存储
基于管道的持久化存储
只要是数据持久化存储,parse方法必须有返回值,也就是return后的内容
1. 基于终端指令的持久化存储
执行输出指定格式进行存储:将爬取到的数据写入不同格式的文件中进行存储,windows终端不能使用txt格式
scrapy crawl 爬虫名称 -o xxx.json
scrapy crawl 爬虫名称 -o xxx.xml
scrapy crawl 爬虫名称 -o xxx.csv
以麦田为例,spider中的代码不变,将返回值写到qiubai.csv中。本地没有,就会自己创建一个。本地有就会追加
- scrapy crawl maitian -o maitian.csv
复制代码
就会在项目目录下看到,生成的文件
查看文件内容
2.基于管道的持久化存储
scrapy框架中已经为我们专门集成好了高效、便捷的持久化操作功能,我们直接使用即可。要想使用scrapy的持久化操作功能,我们首先来认识如下两个文件:
items.py:数据结构模板文件。定义数据属性。
pipelines.py:管道文件。接收数据(items),进行持久化操作。
持久化流程:
① 爬虫文件爬取到数据解析后,需要将数据封装到items对象中。
② 使用yield关键字将items对象提交给pipelines管道,进行持久化操作。
③ 在管道文件中的process_item方法中接收爬虫文件提交过来的item对象,然后编写持久化存储的代码,将item对象中存储的数据进行持久化存储(在管道的process_item方法中执行io操作,进行持久化存储)
④ settings.py配置文件中开启管道
2.1保存到本地的持久化存储
爬虫文件:maitian.py
- import scrapy
- from houseinfo.items import HouseinfoItem # 将item导入
- class MaitianSpider(scrapy.Spider):
- name = 'maitian'
- start_urls = ['http://bj.maitian.cn/zfall/PG100']
- #解析函数
- def parse(self, response):
- li_list = response.xpath('//div[@class="list_wrap"]/ul/li')
- for li in li_list:
- item = HouseinfoItem(
- title = li.xpath('./div[2]/h1/a/text()').extract_first().strip(),
- price = li.xpath('./div[2]/div/ol/strong/span/text()').extract_first().strip(),
- square = li.xpath('./div[2]/p[1]/span[1]/text()').extract_first().replace('㎡',''),
- area = li.xpath('./div[2]/p[2]/span/text()[2]').extract_first().strip().split('\xa0')[0],
- adress = li.xpath('./div[2]/p[2]/span/text()[2]').extract_first().strip().split('\xa0')[2]
- )
- yield item # 提交给管道,然后管道定义存储方式
复制代码
items文件:items.py
- import scrapy
- class HouseinfoItem(scrapy.Item):
- title = scrapy.Field() #存储标题,里面可以存储任意类型的数据
- price = scrapy.Field()
- square = scrapy.Field()
- area = scrapy.Field()
- adress = scrapy.Field()
复制代码
管道文件:pipelines.py
- class HouseinfoPipeline(object):
- def __init__(self):
- self.file = None
- #开始爬虫时,执行一次
- def open_spider(self,spider):
- self.file = open('maitian.csv','a',encoding='utf-8') # 选用了追加模式
- self.file.write(",".join(["标题","月租金","面积","区域","地址","\n"]))
- print("开始爬虫")
- # 因为该方法会被执行调用多次,所以文件的开启和关闭操作写在了另外两个只会各自执行一次的方法中。
- def process_item(self, item, spider):
- content = [item["title"], item["price"], item["square"], item["area"], item["adress"], "\n"]
- self.file.write(",".join(content))
- return item
- # 结束爬虫时,执行一次
- def close_spider(self,spider):
- self.file.close()
- print("结束爬虫")
复制代码
配置文件:settings.py
- #伪装请求载体身份
- USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
- #可以忽略或者不遵守robots协议
- ROBOTSTXT_OBEY = False
- #开启管道
- ITEM_PIPELINES = {
- 'houseinfo.pipelines.HouseinfoPipeline': 300, #数值300表示为优先级,值越小优先级越高
- }
复制代码
四、爬取多级页面
爬取多级页面,会遇到2个问题:
问题1:如何对下一层级页面发送请求?
答:在每一个解析函数的末尾,通过Request方法对下一层级的页面手动发起请求
- # 先提取二级页面url,再对二级页面发送请求。多级页面以此类推
- def parse(self, response):
- next_url = response.xpath('//div[2]/h2/a/@href').extract()[0] # 提取二级页面url
- yield scrapy.Request(url=next_url, callback=self.next_parse) # 对二级页面发送请求,注意要用yield,回调函数不带括号
复制代码
问题2:解析的数据不在同一张页面中,最终如何将数据传递
答:涉及到请求传参,可以在对下一层级页面发送请求的时候,通过meta参数进行数据传递,meta字典就会传递给回调函数的response参数。下一级的解析函数通过response获取item. 先通过 response.meta返回接收到的meta字典,再获得item字典
- # 通过meta参数进行Request的数据传递,meta字典就会传递给回调函数的response参数
- def parse(self, response):
- item = Item() # 实例化item对象
- Item["field1"] = response.xpath('expression1').extract()[0] # 列表中只有一个元素
- Item["field2"] = response.xpath('expression2').extract() # 列表
- next_url = response.xpath('expression3').extract()[0] # 提取二级页面url
- # meta参数:请求传参.通过meta参数进行Request的数据传递,meta字典就会传递给回调函数的response参数
- yield scrapy.Request(url=next_url, callback=self.next_parse,meta={'item':item}) # 对二级页面发送请求
- def next_parse(self,response):
- # 通过response获取item. 先通过 response.meta返回接收到的meta字典,再获得item字典
- item = response.meta['item']
- item['field'] = response.xpath('expression').extract_first()
- yield item #提交给管道
复制代码
案例1:麦田,对所有页码发送请求。不推荐将每一个页码对应的url存放到爬虫文件的起始url列表(start_urls)中。这里我们使用Request方法手动发起请求。
- # -*- coding: utf-8 -*-
- import scrapy
- from houseinfo.items import HouseinfoItem # 将item导入
- class MaitianSpider(scrapy.Spider):
- name = 'maitian'
- start_urls = ['http://bj.maitian.cn/zfall/PG{}'.format(page) for page in range(1,4)]
- #爬取多页
- page = 1
- url = 'http://bj.maitian.cn/zfall/PG%d'
- #解析函数
- def parse(self, response):
- li_list = response.xpath('//div[@class="list_wrap"]/ul/li')
- for li in li_list:
- item = HouseinfoItem(
- title = li.xpath('./div[2]/h1/a/text()').extract_first().strip(),
- price = li.xpath('./div[2]/div/ol/strong/span/text()').extract_first().strip(),
- square = li.xpath('./div[2]/p[1]/span[1]/text()').extract_first().replace('㎡',''),
- area = li.xpath('./div[2]/p[2]/span/text()[2]').re(r'昌平|朝阳|东城|大兴|丰台|海淀|石景山|顺义|通州|西城')[0], # 也可以通过正则匹配提取出来
- adress = li.xpath('./div[2]/p[2]/span/text()[2]').extract_first().strip().split('\xa0')[2]
- )
- ['http://bj.maitian.cn/zfall/PG{}'.format(page) for page in range(1, 4)]
- yield item # 提交给管道,然后管道定义存储方式
- if self.page < 4:
- self.page += 1
- new_url = format(self.url%self.page) # 这里的%是拼接的意思
- yield scrapy.Request(url=new_url,callback=self.parse) # 手动发起一个请求,注意一定要写yield
复制代码
案例2:这个案例比较好的一点是,parse函数,既有对下一页的回调,又有对详情页的回调
- import scrapy
- class QuotesSpider(scrapy.Spider):
- name = 'quotes_2_3'
- start_urls = [
- 'http://quotes.toscrape.com',
- ]
- allowed_domains = [
- 'toscrape.com',
- ]
- def parse(self,response):
- for quote in response.css('div.quote'):
- yield{
- 'quote': quote.css('span.text::text').extract_first(),
- 'author': quote.css('small.author::text').extract_first(),
- 'tags': quote.css('div.tags a.tag::text').extract(),
- }
- author_page = response.css('small.author+a::attr(href)').extract_first()
- authro_full_url = response.urljoin(author_page)
- yield scrapy.Request(authro_full_url, callback=self.parse_author) # 对详情页发送请求,回调详情页的解析函数
-
- next_page = response.css('li.next a::attr("href")').extract_first() # 通过css选择器定位到下一页
- if next_page is not None:
- next_full_url = response.urljoin(next_page)
- yield scrapy.Request(next_full_url, callback=self.parse) # 对下一页发送请求,回调自己的解析函数
- def parse_author(self,response):
- yield{
- 'author': response.css('.author-title::text').extract_first(),
- 'author_born_date': response.css('.author-born-date::text').extract_first(),
- 'author_born_location': response.css('.author-born-location::text').extract_first(),
- 'authro_description': response.css('.author-born-location::text').extract_first(),
复制代码
案例3:爬取www.id97.com电影网,将一级页面中的电影名称,类型,评分,二级页面中的上映时间,导演,片长进行爬取。(多级页面+传参)
- # -*- coding: utf-8 -*-
- import scrapy
- from moviePro.items import MovieproItem
- class MovieSpider(scrapy.Spider):
- name = 'movie'
- allowed_domains = ['www.id97.com']
- start_urls = ['http://www.id97.com/']
- def parse(self, response):
- div_list = response.xpath('//div[@class="col-xs-1-5 movie-item"]')
- for div in div_list:
- item = MovieproItem() item['name'] = div.xpath('.//h1/a/text()').extract_first()
- item['score'] = div.xpath('.//h1/em/text()').extract_first()
- item['kind'] = div.xpath('.//div[@class="otherinfo"]').xpath('string(.)').extract_first()
- item['detail_url'] = div.xpath('./div/a/@href').extract_first()
- #meta参数:请求传参.通过meta参数进行Request的数据传递,meta字典就会传递给回调函数的response参数
- yield scrapy.Request(url=item['detail_url'],callback=self.parse_detail,meta={'item':item})
- def parse_detail(self,response):
- #通过response获取item. 先通过 response.meta返回接收到的meta字典,再获得item字典
- item = response.meta['item']
- item['actor'] = response.xpath('//div[@class="row"]//table/tr[1]/a/text()').extract_first()
- item['time'] = response.xpath('//div[@class="row"]//table/tr[7]/td[2]/text()').extract_first()
- item['long'] = response.xpath('//div[@class="row"]//table/tr[8]/td[2]/text()').extract_first()
- yield item #提交item到管道
复制代码
案例4:稍复杂,可参考链接进行理解:
https://github.com/makcyun/web_scraping_with_python/tree/master/,
https://www.cnblogs.com/sanduzxcvbnm/p/10277414.html
- #!/user/bin/env python
- """
- 爬取豌豆荚网站所有分类下的全部 app
- 数据爬取包括两个部分:
- 一:数据指标
- 1 爬取首页
- 2 爬取第2页开始的 ajax 页
- 二:图标
- 使用class方法下载首页和 ajax 页
- 分页循环两种爬取思路,
- 指定页数进行for 循环,和不指定页数一直往下爬直到爬不到内容为止
- 1 for 循环
- """
- import scrapy
- from wandoujia.items import WandoujiaItem
- import requests
- from pyquery import PyQuery as pq
- import re
- import csv
- import pandas as pd
- import numpy as np
- import time
- import pymongo
- import json
- import os
- from urllib.parse import urlencode
- import random
- import logging
- logging.basicConfig(filename='wandoujia.log',filemode='w',level=logging.DEBUG,format='%(asctime)s %(message)s',datefmt='%Y/%m/%d %I:%M:%S %p')
- # https://juejin.im/post/5aee70105188256712786b7f
- logging.warning("warn message")
- logging.error("error message")
- class WandouSpider(scrapy.Spider):
- name = 'wandou'
- allowed_domains = ['www.wandoujia.com']
- start_urls = ['http://www.wandoujia.com/']
- def __init__(self):
- self.cate_url = 'https://www.wandoujia.com/category/app'
- # 首页url
- self.url = 'https://www.wandoujia.com/category/'
- # ajax 请求url
- self.ajax_url = 'https://www.wandoujia.com/wdjweb/api/category/more?'
- # 实例化分类标签
- self.wandou_category = Get_category()
- def start_requests(self):
- yield scrapy.Request(self.cate_url,callback=self.get_category)
-
- def get_category(self,response):
- # # num = 0
- cate_content = self.wandou_category.parse_category(response)
- for item in cate_content:
- child_cate = item['child_cate_codes']
- for cate in child_cate:
- cate_code = item['cate_code']
- cate_name = item['cate_name']
- child_cate_code = cate['child_cate_code']
- child_cate_name = cate['child_cate_name']
-
- # # 单类别下载
- # cate_code = 5029
- # child_cate_code = 837
- # cate_name = '通讯社交'
- # child_cate_name = '收音机'
-
- # while循环
- page = 1 # 设置爬取起始页数
- print('*' * 50)
- # # for 循环下一页
- # pages = []
- # for page in range(1,3):
- # print('正在爬取:%s-%s 第 %s 页 ' %
- # (cate_name, child_cate_name, page))
- logging.debug('正在爬取:%s-%s 第 %s 页 ' %
- (cate_name, child_cate_name, page))
- if page == 1:
- # 构造首页url
- category_url = '{}{}_{}' .format(self.url, cate_code, child_cate_code)
- else:
- params = {
- 'catId': cate_code, # 大类别
- 'subCatId': child_cate_code, # 小类别
- 'page': page,
- }
- category_url = self.ajax_url + urlencode(params)
- dict = {'page':page,'cate_name':cate_name,'cate_code':cate_code,'child_cate_name':child_cate_name,'child_cate_code':child_cate_code}
-
- yield scrapy.Request(category_url,callback=self.parse,meta=dict)
-
- # # for 循环方法
- # pa = yield scrapy.Request(category_url,callback=self.parse,meta=dict)
- # pages.append(pa)
- # return pages
- def parse(self, response):
- if len(response.body) >= 100: # 判断该页是否爬完,数值定为100是因为无内容时长度是87
- page = response.meta['page']
- cate_name = response.meta['cate_name']
- cate_code = response.meta['cate_code']
- child_cate_name = response.meta['child_cate_name']
- child_cate_code = response.meta['child_cate_code']
- if page == 1:
- contents = response
- else:
- jsonresponse = json.loads(response.body_as_unicode())
- contents = jsonresponse['data']['content']
- # response 是json,json内容是html,html 为文本不能直接使用.css 提取,要先转换
- contents = scrapy.Selector(text=contents, type="html")
- contents = contents.css('.card')
- for content in contents:
- # num += 1
- item = WandoujiaItem()
- item['cate_name'] = cate_name
- item['child_cate_name'] = child_cate_name
- item['app_name'] = self.clean_name(content.css('.name::text').extract_first())
- item['install'] = content.css('.install-count::text').extract_first()
- item['volume'] = content.css('.meta span:last-child::text').extract_first()
- item['comment'] = content.css('.comment::text').extract_first().strip()
- item['icon_url'] = self.get_icon_url(content.css('.icon-wrap a img'),page)
- yield item
-
- # 递归爬下一页
- page += 1
- params = {
- 'catId': cate_code, # 大类别
- 'subCatId': child_cate_code, # 小类别
- 'page': page,
- }
- ajax_url = self.ajax_url + urlencode(params)
-
- dict = {'page':page,'cate_name':cate_name,'cate_code':cate_code,'child_cate_name':child_cate_name,'child_cate_code':child_cate_code}
- yield scrapy.Request(ajax_url,callback=self.parse,meta=dict)
-
- # 名称清除方法1 去除不能用于文件命名的特殊字符
- def clean_name(self, name):
- rule = re.compile(r"[\/\\\:\*\?"\<\>\|]") # '/ \ : * ? " < > |')
- name = re.sub(rule, '', name)
- return name
- def get_icon_url(self,item,page):
- if page == 1:
- if item.css('::attr("src")').extract_first().startswith('https'):
- url = item.css('::attr("src")').extract_first()
- else:
- url = item.css('::attr("data-original")').extract_first()
- # ajax页url提取
- else:
- url = item.css('::attr("data-original")').extract_first()
- # if url: # 不要在这里添加url存在判断,否则空url 被过滤掉 导致编号对不上
- return url
- # 首先获取主分类和子分类的数值代码 # # # # # # # # # # # # # # # #
- class Get_category():
- def parse_category(self, response):
- category = response.css('.parent-cate')
- data = [{
- 'cate_name': item.css('.cate-link::text').extract_first(),
- 'cate_code': self.get_category_code(item),
- 'child_cate_codes': self.get_child_category(item),
- } for item in category]
- return data
- # 获取所有主分类标签数值代码
- def get_category_code(self, item):
- cate_url = item.css('.cate-link::attr("href")').extract_first()
- pattern = re.compile(r'.*/(\d+)') # 提取主类标签代码
- cate_code = re.search(pattern, cate_url)
- return cate_code.group(1)
- # 获取所有子分类标签数值代码
- def get_child_category(self, item):
- child_cate = item.css('.child-cate a')
- child_cate_url = [{
- 'child_cate_name': child.css('::text').extract_first(),
- 'child_cate_code': self.get_child_category_code(child)
- } for child in child_cate]
- return child_cate_url
- # 正则提取子分类
- def get_child_category_code(self, child):
- child_cate_url = child.css('::attr("href")').extract_first()
- pattern = re.compile(r'.*_(\d+)') # 提取小类标签编号
- child_cate_code = re.search(pattern, child_cate_url)
- return child_cate_code.group(1)
- # # 可以选择保存到txt 文件
- # def write_category(self,category):
- # with open('category.txt','a',encoding='utf_8_sig',newline='') as f:
- # w = csv.writer(f)
- # w.writerow(category.values())
复制代码
以上4个案例都只贴出了爬虫主程序脚本,因篇幅原因,所以item、pipeline和settings等脚本未贴出,可参考上面案例进行编写。
五、Scrapy发送post请求
问题:在之前代码中,我们从来没有手动的对start_urls列表中存储的起始url进行过请求的发送,但是起始url的确是进行了请求的发送,那这是如何实现的呢?
解答:其实是因为爬虫文件中的爬虫类继承到了Spider父类中的start_requests(self)这个方法,该方法就可以对start_urls列表中的url发起请求:
- def start_requests(self):
- for u in self.start_urls:
- yield scrapy.Request(url=u,callback=self.parse)
复制代码
注意:该方法默认的实现,是对起始的url发起get请求,如果想发起post请求,则需要子类重写该方法。不过,一般情况下不用scrapy发post请求,用request模块。
例:爬取百度翻译
- # -*- coding: utf-8 -*-
- import scrapy
- class PostSpider(scrapy.Spider):
- name = 'post'
- # allowed_domains = ['www.xxx.com']
- start_urls = ['https://fanyi.baidu.com/sug']
- def start_requests(self):
- data = { # post请求参数
- 'kw':'dog'
- }
- for url in self.start_urls:
- yield scrapy.FormRequest(url=url,formdata=data,callback=self.parse) # 发送post请求
- def parse(self, response):
- print(response.text)
复制代码
六、设置日志等级
- 在使用scrapy crawl spiderFileName运行程序时,在终端里打印输出的就是scrapy的日志信息。
- 日志信息的种类:
ERROR : 一般错误
WARNING : 警告
INFO : 一般的信息
DEBUG : 调试信息
- 设置日志信息指定输出:
在settings.py配置文件中,加入
LOG_LEVEL = ‘指定日志信息种类’即可。
LOG_FILE = 'log.txt'则表示将日志信息写入到指定文件中进行存储。
|
|