美文网首页
scrapy模拟登陆go语言中文网

scrapy模拟登陆go语言中文网

作者: 小董不太懂 | 来源:发表于2019-07-29 17:20 被阅读0次

上一篇已经模拟登陆人人网了。我们这次模拟登陆go语言中文网,并修改个人信息!
目标网站:https://studygolang.com/

分析网站

登录网址:https://studygolang.com/account/login

获得post提交的网址
表单信息
根据上图,应该很容易得到表单信息。安全起见,我们多登陆几次,发现redirect_uri会产生变化,故我们需要到源代码中获得这个redirect_uri。
等待我们登陆成功后,我们想尝试修改个人签名,也就是嵌套提交post请求。废话不多说,直接上代码。
  • 代码框架:
  • go_website_start.py:
    是我设置的启动项。
  • settings.py:
    还是老三样:
# -*- coding: utf-8 -*-

# Scrapy settings for go_website project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
#     https://doc.scrapy.org/en/latest/topics/settings.html
#     https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
#     https://doc.scrapy.org/en/latest/topics/spider-middleware.html

BOT_NAME = 'go_website'

SPIDER_MODULES = ['go_website.spiders']
NEWSPIDER_MODULE = 'go_website.spiders'

LOG_LEVEK = 'WARNING'
# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'go_website (+http://www.yourdomain.com)'

# Obey robots.txt rules
ROBOTSTXT_OBEY = False

# Configure maximum concurrent requests performed by Scrapy (default: 16)
#CONCURRENT_REQUESTS = 32

# Configure a delay for requests for the same website (default: 0)
# See https://doc.scrapy.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
#DOWNLOAD_DELAY = 3
# The download delay setting will honor only one of:
#CONCURRENT_REQUESTS_PER_DOMAIN = 16
#CONCURRENT_REQUESTS_PER_IP = 16

# Disable cookies (enabled by default)
#COOKIES_ENABLED = False

# Disable Telnet Console (enabled by default)
#TELNETCONSOLE_ENABLED = False

# Override the default request headers:
DEFAULT_REQUEST_HEADERS = {
  'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  'Accept-Language': 'en',
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36'
}

# Enable or disable spider middlewares
# See https://doc.scrapy.org/en/latest/topics/spider-middleware.html
#SPIDER_MIDDLEWARES = {
#    'go_website.middlewares.GoWebsiteSpiderMiddleware': 543,
#}

# Enable or disable downloader middlewares
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html
#DOWNLOADER_MIDDLEWARES = {
#    'go_website.middlewares.GoWebsiteDownloaderMiddleware': 543,
#}

# Enable or disable extensions
# See https://doc.scrapy.org/en/latest/topics/extensions.html
#EXTENSIONS = {
#    'scrapy.extensions.telnet.TelnetConsole': None,
#}

# Configure item pipelines
# See https://doc.scrapy.org/en/latest/topics/item-pipeline.html
#ITEM_PIPELINES = {
#    'go_website.pipelines.GoWebsitePipeline': 300,
#}

# Enable and configure the AutoThrottle extension (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/autothrottle.html
#AUTOTHROTTLE_ENABLED = True
# The initial download delay
#AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
#AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
#AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
#AUTOTHROTTLE_DEBUG = False

# Enable and configure HTTP caching (disabled by default)
# See https://doc.scrapy.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
#HTTPCACHE_ENABLED = True
#HTTPCACHE_EXPIRATION_SECS = 0
#HTTPCACHE_DIR = 'httpcache'
#HTTPCACHE_IGNORE_HTTP_CODES = []
#HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'

  • go_spider.py:
# -*- coding: utf-8 -*-
import scrapy


class GoSpiderSpider(scrapy.Spider):
    name = 'go_spider'
    allowed_domains = ['studygolang.com']
    start_urls = ['https://studygolang.com/account/login']
    login_url = 'https://studygolang.com/account/login'
    edit_url = 'https://studygolang.com/account/edit'
    def parse(self, response):
        laji = response.xpath('//*[@id="wrapper"]/div/div[2]/div[1]/div/form/input/@value').get()
        formdata = {
            'redirect_uri': laji,
            'username': 'xxxxxx',
            'passwd': 'xxxxxxxx',
            'remember_me': '1'
        }
        request = scrapy.FormRequest(url=self.login_url,formdata=formdata,callback=self.parse_detail)
        yield request

    def parse_detail(self,response):
        user_name = response.xpath('//*[@id="user_menu"]/a/text()').get()
        print(user_name)
        if user_name:
            yield scrapy.Request(url=self.edit_url,callback=self.parse_sign)
            print('登陆成功')
        else:
            print('登录失败')

    def parse_sign(self,response):
        # print(response.text)
        formdata = {
            'email': '1xxxxxxx@qq.com',
            'name': '',
            'city': '',
            'company': '',
            'website': '',
            'monlog': '我是爬虫修改的啊',
            'introduce': ''
        }
        request = scrapy.FormRequest(url=self.edit_url,formdata=formdata)
        yield request
  • 运行结果:
    这是原本的签名
    开始执行代码
    修改后的签名

相关文章

网友评论

      本文标题:scrapy模拟登陆go语言中文网

      本文链接:https://www.haomeiwen.com/subject/bcxirctx.html