Scrapy初探四2020-08-29

scrapy模拟登陆

那么对于scrapy来说,也是有两个方法模拟登陆

  • 直接携带cookie
  • 直接发送post请求的url地址,带上信息发送请求

scrapy模拟登陆人人网

  • 携带cookie
#爬虫内容
import scrapy


class CookieloginSpider(scrapy.Spider):
    name = 'cookielogin'
    allowed_domains = ['renren.com']
    start_urls = ['http://www.renren.com/975020754/profile']

    def start_requests(self):
        cookies = 'anonymid=kecwg3kgxsir2u; depovince=ZGQT; _r01_=1; taihe_bi_sdk_uid=e800d28f1c62ee2d1573ae3e9650f8b0; _de=47C2967B427B7B8644B0A57815E527E7696BF75400CE19CC; JSESSIONID=abcgRl8M4GdXc8k_tj4qx; ick_login=ab0b02c9-ec87-41de-a57c-55c6a4916ef2; taihe_bi_sdk_session=3e2b48a0231209751903e72388f61489; t=5f23d0b5b537fbb4c9a0b076955627e24; societyguester=5f23d0b5b537fbb4c9a0b076955627e24; id=975020754; xnsid=c7da711c; ver=7.0; loginfrom=null; wp_fold=0; jebecookies=2bce41a3-8abb-4eed-8646-0591daf01a59|||||'

        cookies = cookies.split('; ')

        cookies = {i.split('=')[0]:i.split('=')[1] for i in cookies}

        yield scrapy.Request(
            url=self.start_urls[0],
            cookies=cookies,
            callback=self.parse
        )

    def parse(self, response):
        with open('renren.html', 'w', encoding='utf-8') as f:
            f.write(response.body.decode())

scrapy模拟登陆github

  • 发送post请求
# -*- coding: utf-8 -*-
import scrapy
# https://github.com/login 起始的url地址
# https://github.com/session 发送post表单请求
'''
commit: Sign in
authenticity_token: daEuLUefyDXj8MLw/cD5JjYrcqNbZe/FiIgEnDtpKwF1CYefkaus9VGNMQqJMkR2DVAQMW9irgiPbU2a/k+89Q==
ga_id: 287622012.1592305586
login: LogicJerry
password: 123456
webauthn-support: supported
webauthn-iuvpaa-support: supported
return_to: 
required_field_84fa: 
timestamp: 1598532226351
timestamp_secret: cbc64832cf60571a5dc3649c0cb1b707c5e598ea75887850681ae0183bb3e519
'''

class GithubSpider(scrapy.Spider):
    name = 'github'
    allowed_domains = ['github.com']
    start_urls = ['https://github.com/login']

    def parse(self, response):
        commit = 'Sign in'
        authenticity_token = response.xpath("//input[@name='authenticity_token']/@value").extract_first()
        # ga_id = response.xpath("//input[@name='ga_id']/@value").extract_first()
        login = 'LogicJerry'
        password = '12122121zxl'
        timestamp = response.xpath("//input[@name='timestamp']/@value").extract_first()
        timestamp_secret = response.xpath("//input[@name='timestamp_secret']/@value").extract_first()

        # 定义一个字典提交数据
        data = {
            'commit': commit,
            'authenticity_token': authenticity_token,
            # 'ga_id': ga_id,
            'login': login,
            'password': password,
            'webauthn-support': 'supported',
            'webauthn-iuvpaa-support': 'unsupported',
            'timestamp': timestamp,
            'timestamp_secret': timestamp_secret,
        }

    # 提交数据发送请求
        yield scrapy.FormRequest(
            # 提交的地址
            url='https://github.com/session',
            # 提交数据
            formdata=data,
            # 响应方法
            callback=self.after_login
        )

    def after_login(self,response):

        # print(response)
        # 保存文件
        with open('github3.html','w',encoding='utf-8') as f:
            f.write(response.body.decode())

scrapy自动登陆

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


class Github2Spider(scrapy.Spider):
    name = 'github2'
    allowed_domains = ['github.com']
    start_urls = ['https://github.com/login']

    def parse(self, response):

        yield scrapy.FormRequest.from_response(
            # 请求响应结果
            response=response,
            # 提交数据
            formdata={'login_field':'LogicJerry','password':'12122121zxl'},
            # 回调函数
            callback=self.after_login
        )

    def after_login(self,response):

        # print(response)

        # 保存文件
        with open('github4.html','w',encoding='utf-8') as f:
            f.write(response.body.decode())

Scrapy下载图片

下载图片案例 爬取汽车之家图片

scrapy为下载item中包含的文件提供了一个可重用的item pipelines,这些pipeline有些共同的方法和结构,一般来说你会使用Files Pipline或者Images Pipeline

爬取汽车家
https://www.autohome.com.cn/65/#levelsource=000000000_0&pvareaid=101594

选择使用scrapy内置的下载文件的方法

  • 1:避免重新下载最近已经下载过的数据
  • 2:可以方便的指定文件存储的路径
  • 3:可以将下载的图片转换成通用的格式。如:png,jpg
  • 4:可以方便的生成缩略图
  • 5:可以方便的检测图片的宽和高,确保他们满足最小限制
  • 6:异步下载,效率非常高

下载文件的 Files Pipeline

使用Files Pipeline下载文件,按照以下步骤完成:

  • 定义好一个Item,然后在这个item中定义两个属性,分别为file_urls以及files。files_urls是用来存储需要下载的文件的url链接,需要给一个列表
  • 当文件下载完成后,会把文件下载的相关信息存储到item的files属性中。如下载路径、下载的url和文件校验码等
  • 在配置文件settings.py中配置FILES_STORE,这个配置用来设置文件下载路径
  • 启动pipeline:在ITEM_PIPELINES中设置scrapy.piplines.files.FilesPipeline:1

下载图片的 Images Pipeline

使用images pipeline下载文件步骤:

  • 定义好一个Item,然后在这个item中定义两个属性,分别为image_urls以及images。image_urls是用来存储需要下载的文件的url链接,需要给一个列表
  • 当文件下载完成后,会把文件下载的相关信息存储到item的images属性中。如下载路径、下载的url和图片校验码等
  • 在配置文件settings.py中配置IMAGES_STORE,这个配置用来设置文件下载路径
  • 启动pipeline:在ITEM_PIPELINES中设置scrapy.pipelines.images.ImagesPipeline:1

你可能感兴趣的:(Scrapy初探四2020-08-29)