day7、scrapy-post请求使用

百度翻译的post请求

baidu.py

# -*- coding: utf-8 -*-
import scrapy


class BaiduSpider(scrapy.Spider):
    name = 'baidu'
    allowed_domains = ['baidu.com']
    # start_urls = ['http://baidu.com/']

    def start_requests(self):
        #当下载器被调起来,首先会调用这个方法,默认在这方法发起的get请求
        post_url = "https://fanyi.baidu.com/sug"
        data = {
            "kw": "hello"
        }
        #调用下载器发起post请求
        yield scrapy.FormRequest(url=post_url, formdata=data, callback=self.parse_post)

    def parse_post(self, response):
        print("+++++++++++++++")
        print(response.text)

你可能感兴趣的:(day7、scrapy-post请求使用)