python scrapy 笔记

2018-05-08  本文已影响0人  任我笑笑

url https://anaconda.org/conda-forge/scrapy

文档 https://doc.scrapy.org/en/latest/index.html

安装

conda install -c conda-forge scrapy

跑例子

scrapy runspider xxxxxxxxxx.py

新建项目

scrapy startproject XXXXX XXXXX代表你项目的名字

小白入门

参考知乎登录 https://zhuanlan.zhihu.com/p/25672345

不过文章已经是一年前,现在是有文字验证的,不管用
理论上来说,应该是用浏览器登录后,查看cookie,然后把cookie放到代码里直接去爬

import scrapy

class Login(scrapy.Spider):

    name = 'zhihu_login'

    start_urls = ['https://www.zhihu.com' ]
    custom_settings = {'USER_AGENT': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3026.3 Safari/537.36'}


    def parse(self, response):
        formadata = {
            'password': 'xxxxxxx',
            'phone_num': 'yyyyy',
            'email': '邮箱号二选一'
        }
        return scrapy.FormRequest.from_response(
                                  url='https://www.zhihu.com/login/{}'.format('phone_num'
                                                                          if formadata['phone_num'] else 'email'), # post 的网址
                                  method="POST", # 也是默认值, 其实不需要指定
                                  response=response, 
                                  formxpath='//form[1]', # 使用第一个form, 其实就是默认的, 这里明确写出来
                                  formdata=formadata, # 我们填写的表单数据
                                  callback=self.after_login, # 登录完成之后的处理
                                  dont_click=True)
上一篇 下一篇

猜你喜欢

热点阅读