Scrapy 之如何发送post请求
import scrapy import json class PostSpider(scrapy.Spider): name = 'post' # allowed_domains = ['www.xxx.com'] start_urls = ['https://fanyi.baidu.com/sug'] def start_requests(self): #默认对start_urls列表里的每一条url发起get请求,如果想发起post请求,必须重写父类的start_requests方法 data = { 'kw':'dog' } for url in self.start_urls: yield scrapy.FormRequest(url=url,formdata=data,callback=self.parse)
#post请求必须由yield返回FormRequest对象,第一个参数为url,第二个参数为请求所带参数,第三个参数为回调函数 def parse(self, response): data = json.loads(response.text) print(data.get("data"))