python_多线程爬虫

多线程爬虫:并发处理结果,基于以前的生产者与消费者模型,交叉执行,可以让程序的运行效果更好

需要导入新的模块:import threading

创建线程:

class a(threading.Thread):

  def __init__(self):

    threading.Thread.__init__()

  def run():    #线程要做什么事

import threading
class A(threading.Thread):
    def __init__(self):         #初始化
        threading.Thread.__init__(self)
    def run(self):
        for i in range(0,10):
            print("我是线程A")
class B(threading.Thread):
    def __init__(self):         #初始化
        threading.Thread.__init__(self)
    def run(self):
        for i in range(0,10):
            print("我是线程B")
t1 = A()
t1.start()
t2 = B()
t2.start()

 在使用fd抓包后如果软件一直开启,则会报错requests.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)

解决方法:

import ssl

ssl._create_default_https_context = ssl._create_unverified_context

二:如果使用的是requests模块,则只需加入verify=False,但有可能会出现安全警告,只需要加入

from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

 三:全局取消

import ssl
import urllib2

ssl._create_default_https_context = ssl._create_unverified_context
print urllib2.urlopen("https://www.12306.cn/mormhweb/").read()

四:

import ssl 
import urllib2 

# This restores the same behavior as before. 
context = ssl._create_unverified_context()
print urllib2.urlopen("https://www.12306.cn/mormhweb/", context=context).read()

 安装scrapy框架,可以参考:https://blog.csdn.net/zer021/article/details/80659157

原文地址:https://www.cnblogs.com/Alom/p/11206329.html