爬虫实战 -- (爬取证券期货市场失信记录平台)

本文介绍了如何使用selenium爬取动态网页证券期货市场失信记录平台的数据,针对页面动态加载及验证码问题,采取截图解析验证码的方式,实现了数据的抓取。爬取过程包括指定姓名和机构代码,通过selenium模拟浏览行为,并利用pytesseract模块处理动态验证码。
摘要由CSDN通过智能技术生成

这里我们要通过实际展示爬取证券期货市场失信记录平台上的搜索数据。

页面:http://shixin.csrc.gov.cn/honestypub  如下:

我们现在要通过爬虫给定一个 姓名,机构代码 ,爬取获得的结果

这里主要说明两点:

1. 这是一个动态网页,因此我采用 selenium 方法。

2.这里的验证码图片并不在源码内,因此前面的通过 css 选择器直接下载的方式是不行的。并且给定的验证码图片的连接即使一样,生成的验证码也是随机的,因此我们并不能通过源码中给定链接下载验证码这种方式。

验证码部分在源代码解析如下:

这是一个链接,并且这个链接生成的验证码是会变动,并不能通过这个链接下载到我们要的验证码图形。因此我的处理方案是直接截图下来进行解析。解析采用 pytesseract 模块,但其实里面源码就是调用 tesseract ,因此要安装这个。

爬虫完整代码如下:

# -*- coding:utf-8 -*-
import json
from PIL import Image
import pytesseract
from selenium import webdriver
# 为实现异步请求,增加 monkey
from gevent import monkey
from gevent.pywsgi import WSGIServer
#monkey.patch_all()

#from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import ui
#from selenium.webdriver.common.keys import Keys
#from selenium.webdriver.firefox.options import Options
import sys,os
reload(sys)
sys.setdefaultencoding("utf-8")
import datetime
import logging
import time
from io import BytesIO
# 因为要用docker 服务,所以不采用任何写入操作
#logger_file = logging.getLogger("firefox")
logger_console = logging.getLogger("firefox_console")
formatter = logging.Formatter('%(process)s -- %(asctime)s %(levelname)s:    %(message)s')
#file_handler = logging.FileHandler("firefox.log")
#file_handler.setFormatter(formatter)
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(formatter)

#logger_file.addHandler(file_handler)
logger_console.addHandler(console_handler)
#logger_file.setLevel(logging.INFO)
logger_console.setLevel(logging.INFO)

from flask import Flask, request
from flask import Response

from flask_cors import CORS
from flask_restful import reqparse, abort, Api, Resource
from conf.config import APPID

#ff_option = Options()
#ff_option.add_argument('-headless')
options = webdriver.FirefoxOptions()
options.set_headless()
# 设置用户代理
#profile = webdriver.FirefoxProfile()
#profile.set_preference("general.useragent.enable_overrides",True)
#profile.set_preference("general.useragent.override","Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Mobile Safari/537.36")
#profile.update_preferences()

import platform
SYSTEM = platform.system()
if SYSTEM == "Windows":
    chromedriver = "C:\Program Files\Mozilla Firefox\geckodriver.exe"
    #chromedriver = r"E:\phantomjs-2.1.1-windows\bin\phantomjs.exe"
else:
    chromedriver = "driver/geckodriver"
    
# 验证码最大解析次数
MAX_RETRY = 10
RESTART_FLAG = 0
PAGE_LOAD_TIME = 20
LOGIN_WEB = "http://shixin.csrc.gov.cn/honestypub"

driver = webdriver.Firefox(executable_path=chromedriver,firefox_options=options) #,firefox_profile = profile)
wait = ui.WebDriverWait(driver,10)
driver.get(LOGIN_WEB)
wait.until(lambda driver: driver.find_element_by_id("objName"))
# 设置页面加载时间
driver.set_page_load_timeout(PAGE_LOAD_TIME)
cache = []

app = Flask(__name__)
CORS(app, resources=r'/*')
# 使用 restful 扩展库
api = Api(app)
# 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值