栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

scrapy中使用Fromquest

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

scrapy中使用Fromquest

scrapy官方自带的访问方法Request,post的话是scrapy.FormRequest

import scrapy
import requests

class LogingitSpider(scrapy.Spider):
    name = 'logingit'
    allowed_domains = ['github.com']
    # 登陆界面的URL
    login_url = 'https://github.com/login'
    # POST表单数据的URL
    post_url = 'https://github.com/session'
    # 登陆后URL
    logined_url = 'https://github.com/settings/profile'

    def start_requests(self):
        """
        获取登陆页面源码
        """
        return [scrapy.Request(url=self.login_url,
                              callback=self.login,
                              headers=self.settings.get('DEFAULT_REQUEST_HEADERS'))]

    def login(self, response):
        """
        使用FromRequest模拟登陆Github
        """
        # 提取POST验证参数 authenticity_token
        authcode = response.xpath('//*[@id="login"]/form/input[2]/@value').extract_first()
        if authcode:
            self.logger.debug("Auth Token: %s" %authcode)
            post_data = {
                'commit': 'Sign in',
                'utf8': '✓',
                'authenticity_token': authcode,
                'login': self.settings.get('ACCOUNT'),
                'password': self.settings.get('PASSWORD')
            }
            return [scrapy.FormRequest(url=self.post_url,
                                      formdata=post_data,
                                      headers=self.settings.get('DEFAULT_REQUEST_HEADERS'),
                                      callback=self.check)]
        else:
            return [scrapy.Request(url=self.login_url, callback=self.login)]

    def check(self, response):
        """
        验证登陆是否成功
        """
        avatar = response.css('#user-links > li:nth-child(3) > details > summary > img::attr(src)').extract_first()
        if avatar:
            content = requests.get(url=avatar.split('?')[0]).content
            with open('./utils/acatar.jpg', 'wb') as f:
                f.write(content)
            print('Successfully Login!')
        pass


    def parse(self, response):
        pass

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1018473.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号