一.POST请求

class Postspider(scripy.Spider):
name = "post"
# allowed_domains = ["www.xxx.com"]
start_urls = ["https.//fanyi.baidu.com/sug"] def start_requests(self):
data = {
"kw": "dog"
}
for url in self.start_urls:
yield scrapy.FormRequest(url=url, formdata=data, calback=self.parse) def parse(self, response):
print(response.text)

二.请求传参

  - 在某些情况下,我们爬取的数据不在同一个页面中,例如,我们爬取一个电影网站,电影的名称,评分在一级页面,而要爬取的其他电影详情在其二级子页面中。这时我们就需要用到请求传参。

ex:爬取www.id97.com电影网,将一级页面中的电影名称,类型,评分一级二级页面中的上映时间,导演,片长进行爬取。

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()
#xpath(string(.))表示提取当前节点下所有子节点中的数据值(.)表示当前节点
item['kind'] = div.xpath('.//div[@class="otherinfo"]').xpath('string(.)').extract_first()
item['detail_url'] = div.xpath('./div/a/@href').extract_first()
#请求二级详情页面,解析二级页面中的相应内容,通过meta参数进行Request的数据传递
yield scrapy.Request(url=item['detail_url'],callback=self.parse_detail,meta={'item':item}) def parse_detail(self,response):
#通过response获取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()
#提交item到管道
yield item

items.py:

 -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.html import scrapy class MovieproItem(scrapy.Item):
# define the fields for your item here like:
name = scrapy.Field()
score = scrapy.Field()
time = scrapy.Field()
long = scrapy.Field()
actor = scrapy.Field()
kind = scrapy.Field()
detail_url = scrapy.Field()

管道文件:

# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html import json
class MovieproPipeline(object):
def __init__(self):
self.fp = open('data.txt','w')
def process_item(self, item, spider):
dic = dict(item)
print(dic)
json.dump(dic,self.fp,ensure_ascii=False)
return item
def close_spider(self,spider):
self.fp.close()

三、log日志相关

  - 在使用scrapy crawl spiderFileName运行程序时,在终端里打印输出的就是scrapy的日志信息。

  - 日志信息的种类:

        ERROR : 一般错误

        WARNING : 警告

        INFO : 一般的信息

        DEBUG : 调试信息

        默认的显示级别是DEBUG

  - 设置日志信息指定输出:

    在settings.py配置文件中,加入LOG_LEVEL = ‘指定日志信息种类’即可。LOG_FILE = 'log.txt'则表示将日志信息写入到指定文件中进行存储。

爬虫之post请求与请求传参的更多相关文章

  1. jmeter运行脚本后,请求偶发性的传参错误

    问题现象:jmeter写好脚本后,请求偶发性的传参错误 排查过程:1.结合报错返回值,看是不是线程并发引起: 2.排除线程并发引起后,看看是不是取值策略:如果是参数化,看看是不是每次迭代,每次都取唯一 ...

  2. jmeter post请求在终端传参,并且指定请求参数文件

    命令:jmeter -n -t   jmeter_1.jmx   -l   report/report30.jtl    -Jnum=3000 -e -o   webresult/3000result ...

  3. RestTemplate post请求使用map传参 Controller 接收不到值的解决方案 postForObject方法源码解析.md

    结论 post方法中如果使用map传参,需要使用MultiValueMap来传递 RestTemplate 的 postForObject 方法有四个参数 String url => 顾名思义 ...

  4. get请求中url传参中文乱码问题

    在项目中经常会遇到中文传参数,在后台接收到乱码问题.那么在遇到这种情况下我们应该怎么进行处理让我们传到后台接收到的参数不是乱码是我们想要接收的到的,下面就是我的一些认识和理解. 一:get请求url中 ...

  5. get请求中url传参中文乱码问题--集锦

    一:get请求url中带有中文参数,有三种方式进行处理防止中文乱码 1.如果使用tomcat作为服务器,那么修改tomcat配置文件conf/server.xml中,在  <Connector  ...

  6. 【axios】get/post请求params/data传参总结

    axios中get/post请求方式 1. 前言 最近突然发现post请求可以使用params方式传值,然后想总结一下其中的用法. 2.1 分类 get请求中没有data传值方式 2.2 get请求 ...

  7. Ajax的请求方式几传参的区别

    Get,Post,Put,Delete请求(ajax)方式的不通. http://blog.jobbole.com/99854/

  8. Jmeter 接口测试-请求 Headers 与传参方式

    1.添加信息表头. 注意:1.使用Parameters时,Content-Type要么不传,要么传application/x-www-form-urlencoded,因为不传时默认值就是applica ...

  9. asp.net Get和Post传参和接收参数

    asp.netGet和Post传参和接收参数 Get请求: 对于传参:test.aspx?name=%e5%bc%a0%e4%b8%89 接收参数的方法: Request.QueryString[&q ...

  10. axios常见传参方式

    1:get请求 一般发送请求是这么写 axios.get('/user?id=12345&name=user') .then(function (res) { console.log(res) ...

随机推荐

  1. 30段极简Python代码

    Python 是机器学习最广泛采用的编程语言,它最重要的优势在于编程的易用性.如果读者对基本的 Python 语法已经有一些了解,那么这篇文章可能会给你一些启发.作者简单概览了 30 段代码,它们都是 ...

  2. PHP 相关性系数计算

    相关系数公式 参考:https://baike.baidu.com/item/相关系数 PHP 实现代码 public static function calc($list) { $cv = []; ...

  3. docker安装并运行mongo

    拉镜像: [mall@VM_0_7_centos ~]$ sudo docker pull mongo:3.2 [sudo] password for mall: 3.2: Pulling from ...

  4. 开发人员不得不知的MySQL索引和查询优化

    转载:https://blog.csdn.net/enmotech/article/details/88809822 本文主要总结了慢查询优化的过程中常用的以及不合理的操作,适合有 MySQL 基础的 ...

  5. ThinkPHP5最新URL访问:PATH_INFO和兼容模式

    https://www.jianshu.com/p/c43fb5817ae1 http://tp5.com/index.php?s=USER/manger_user/add&n=2000&am ...

  6. tensorflow2.0手写数字识别

    import tensorflow as tf import matplotlib.pyplot as plt import numpy as np datapath = r'D:\data\ml\m ...

  7. laravel相关知识点

    参考地址:http://note.youdao.com/noteshare?id=9899f8328427de449390230c35489934

  8. springboot2 配置 https

    package cn.xiaojf.aibus.configure; import org.apache.catalina.Context; import org.apache.catalina.co ...

  9. 了解一下JVM和GC工作机制

    题外话:很久没有写博客了,事情颇多,今天空闲下来,学习一下顺便写一下自己的了解,机会总是留给有准备的人,所以平常一定要注意知识的巩固和积累.知识的深度也要有一定的理解,不比别人知道的多,公司干嘛选你? ...

  10. Nvidia Jetson TX2开发板学习历程( 2 )- 更换pip源,提高下载速度

    通过将pip的源更换为国内源,来提高下载速度,这也将成为今后学习过程下载Python包的基础,建议前期一定要完成! 知名的国内源 清华:https://pypi.tuna.tsinghua.edu.c ...