1 2 Fork me on GitHub 6

scrapy (3)- post请求

”python爬虫系列“目录:

'''
QiuBai.py 爬虫文件
'''

import scrapy


class PostSpider(scrapy.Spider):
    name = 'post'
    allowed_domains = ['cn.bing.com']
    # start_urls = ['http://cn.bing.com/']
    
    # 爬虫一启动,就是直接运行start_requests这个函数,这个函数发送的是get请求,如果现在你想直接发送post请求,所以你应该重写这个方法
    # def start_requests(self):
    #   for url in start_urls:
    #       yield scrapy.Request(url=url, callback=self.parse)
    
    def start_requests(self):
        post_url = 'https://cn.bing.com/ttranslationlookup?&IG=043A7C9FF4554AF19C3E9A576C1044CE&IID=translator.5036.3'
        data = {
            'from': 'zh-CHS',
            'to': 'en',
            'text': '高原',
        }
        yield scrapy.FormRequest(url=post_url, formdata=data, callback=self.parse)

    def parse(self, response):
        print('*' * 100)
        print(response.text)
        print('*' * 100)

posted @ 2020-05-26 13:15  peng_li  阅读(305)  评论(2编辑  收藏  举报
1