scrapy实战(一)--知乎问答

知乎问答

使用scrapy框架 mysql存储数据
爬取网页 https://www.zhihu.com/

创建项目

  1. 创建项目虚拟环境
    1
    mkvirtualenv spider --python=python3
  2. 安装scrapy
1
pip install -i https://pypi.douban.com/simple/ scrapy

由于此下载依赖包很多,如出现某个包下载出错,重复执行该命令即可。

  1. 创建项目
1
scrapy startproject spider_task
  1. 创建爬虫
1
2
cd spider_task
scrapy genspider zhihu zhihu.com
  1. 通过pycharm打开刚才创建的项目

  2. 导入新创建的虚拟环境

  3. 测试是否创建成功
    启动爬虫,在命令行中输入

    1
    scrapy crawl zhihu

    如果出现 no module named ‘win32api’ 执行

1
pip install -i https://pypi.douban.com/simple/ pypiwin32

启动成功
在这里插入图片描述

项目配置

  • 新建main函数
    在pycharm项目中添加main函数代替命令行执行启动爬虫操作,方便断点测试.
1
2
3
4
5
6
7
from scrapy.cmdline import execute

import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)))
execute(["scrapy","crawl","zhihu"])
  • 修改settings.py文件
    ROBOTSTXT_OBEY = False

模拟登入

安装selenium和chromdriver

  • 安装selenium
    1
    pip install selenium
  • 根据chrome浏览器 下载相应版本的chromedriver
  • 可通过点击谷歌浏览器『帮助』选择 『关于 google chrome』查看当前版本
    有两个下载地址:

http://chromedriver.storage.googleapis.com/index.html

https://npm.taobao.org/mirrors/chromedriver/

模拟登入代码
最新的知乎已经可以检测是否使用selenium
所以本阶段采用手动开启chrom debug模式避开检测

  • terminal中启动:
1
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222
  • 修改代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# -*- coding: utf-8 -*-
import scrapy
import time


from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys

from SpiderTask.utils import common

passwd = common.decrypt(b'c82b377c2a556ac25c38e35783769612')


class ZhihuSpider(scrapy.Spider):
name = 'zhihu'
allowed_domains = ['www.zhihu.com/']
start_urls = ['http://www.zhihu.com/']



def start_requests(self):

chrome_option = Options()
chrome_option.add_argument("--disable-extensions")
chrome_option.add_experimental_option("debuggerAddress", "127.0.0.1:9222")

browser = webdriver.Chrome(executable_path='/Applications/chromedriver',chrome_options=chrome_option)
browser.get('https://www.zhihu.com/signin')
browser.find_element_by_xpath('//*[@id="root"]/div/main/div/div/div[1]/div/form/div[1]/div[2]').click()
browser.find_element_by_xpath(
'//*[@id="root"]/div/main/div/div/div[1]/div/form/div[2]/div[2]/div[1]/input').send_keys(Keys.CONTROL + "a")
browser.find_element_by_xpath('//*[@id="root"]/div/main/div/div/div[1]/div/form/div[2]/div[2]/div[1]/input').send_keys("15872152010")
browser.find_element_by_xpath(
'//*[@id="root"]/div/main/div/div/div[1]/div/form/div[3]/div/div[1]/input').send_keys(Keys.CONTROL + "a")
browser.find_element_by_xpath('//*[@id="root"]/div/main/div/div/div[1]/div/form/div[3]/div/div[1]/input').send_keys(passwd)
browser.find_element_by_xpath('//button[@class="Button SignFlow-submitButton Button--primary Button--blue"]').click()

time.sleep(60)

测试是否通过selenium填写表单

Powered by Hexo and Hexo-theme-hiker

Copyright © 2013 - 2021 Inner peace All Rights Reserved.

UV : | PV :