Python多線程爬蟲實(shí)戰(zhàn)_爬取糗事百科段子的實(shí)例
多線程爬蟲:即程序中的某些程序段并行執(zhí)行,
合理地設(shè)置多線程,可以讓爬蟲效率更高
糗事百科段子普通爬蟲和多線程爬蟲
分析該網(wǎng)址鏈接得出:
https://www.qiushibaike.com/8hr/page/頁(yè)碼/
多線程爬蟲也就和JAVA的多線程差不多,直接上代碼
'''
#此處代碼為普通爬蟲
import urllib.request
import urllib.error
import re
headers = ("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36")
opener = urllib.request.build_opener()
opener.addheaders = [headers]
urllib.request.install_opener(opener)
for i in range(1,2):
url = "https://www.qiushibaike.com/8hr/page/"+str(i)+"/"
pagedata = urllib.request.urlopen(url).read().decode("utf-8","ignore")
pattern = '<div class="content">.*?<span>(.*?)</span>(.*?)</div>'
datalist = re.compile(pattern,re.S).findall(pagedata)
for j in range(0,len(datalist)):
print("第"+str(i)+"頁(yè)第"+str(j)+"個(gè)段子內(nèi)容是:")
print(datalist[j])
'''
'''
#此處為多線程介紹代碼
import threading #導(dǎo)入多線程包
class A(threading.Thread): #創(chuàng)建一個(gè)多線程A
def __init__(self): #必須包含的兩個(gè)方法之一:初始化線程
threading.Thread.__init__(self)
def run(self): #必須包含的兩個(gè)方法之一:線程運(yùn)行方法
for i in range(0,11):
print("我是線程A")
class B(threading.Thread): #創(chuàng)建一個(gè)多線程A
def __init__(self): #必須包含的兩個(gè)方法之一:初始化線程
threading.Thread.__init__(self)
def run(self): #必須包含的兩個(gè)方法之一:線程運(yùn)行方法
for i in range(0,11):
print("我是線程B")
t1 = A() #線程實(shí)例化
t1.start() #線程運(yùn)行
t2 = B()
t2.start()
'''
#此處為修改后的多線程爬蟲
#使用多線程進(jìn)行奇偶頁(yè)的爬取
import urllib.request
import urllib.error
import re
import threading
headers = ("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36")
opener = urllib.request.build_opener()
opener.addheaders = [headers]
urllib.request.install_opener(opener)
class one(threading.Thread): #爬取奇數(shù)頁(yè)內(nèi)容
def __init__(self):
threading.Thread.__init__(self)
def run(self):
for i in range(1,12,2):
url = "https://www.qiushibaike.com/8hr/page/"+str(i)+"/"
pagedata = urllib.request.urlopen(url).read().decode("utf-8","ignore")
pattern = '<div class="content">.*?<span>(.*?)</span>(.*?)</div>'
datalist = re.compile(pattern,re.S).findall(pagedata)
for j in range(0,len(datalist)):
print("第"+str(i)+"頁(yè)第"+str(j)+"段子內(nèi)容為:")
print(datalist[j])
class two(threading.Thread): #爬取奇數(shù)頁(yè)內(nèi)容
def __init__(self):
threading.Thread.__init__(self)
def run(self):
for i in range(2,12,2):
url = "https://www.qiushibaike.com/8hr/page/"+str(i)+"/"
pagedata = urllib.request.urlopen(url).read().decode("utf-8","ignore")
pattern = '<div class="content">.*?<span>(.*?)</span>(.*?)</div>'
datalist = re.compile(pattern,re.S).findall(pagedata)
for j in range(0,len(datalist)):
print("第"+str(i)+"頁(yè)第"+str(j)+"段子內(nèi)容為:")
print(datalist[j])
t1 = one()
t2 = two()
t1.start()
t2.start()
以上這篇Python多線程爬蟲實(shí)戰(zhàn)_爬取糗事百科段子的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
對(duì)pandas中時(shí)間窗函數(shù)rolling的使用詳解
今天小編就為大家分享一篇對(duì)pandas中時(shí)間窗函數(shù)rolling的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-11-11
詳解Python中open()函數(shù)指定文件打開方式的用法
well,我們這里所指的文件打開方式并不是指調(diào)用什么應(yīng)用程序去打開某個(gè)文件,而是只讀只寫或者二進(jìn)制等的打開方式,這里我們就來詳解Python中open()函數(shù)指定文件打開方式的用法2016-06-06
Python Tensor FLow簡(jiǎn)單使用方法實(shí)例詳解
這篇文章主要介紹了Python Tensor FLow簡(jiǎn)單使用方法,結(jié)合實(shí)例形式詳細(xì)分析了Tensor FLow相關(guān)概念、原理、用法與操作注意事項(xiàng),需要的朋友可以參考下2020-01-01
保姆級(jí)官方y(tǒng)olov7訓(xùn)練自己的數(shù)據(jù)集及項(xiàng)目部署詳解
最近使用了YOLOv7訓(xùn)練自己的數(shù)據(jù)集,接下來簡(jiǎn)單記錄一下項(xiàng)目的部署,這篇文章主要給大家介紹了關(guān)于保姆級(jí)官方y(tǒng)olov7訓(xùn)練自己的數(shù)據(jù)集及項(xiàng)目部署的相關(guān)資料,需要的朋友可以參考下2022-08-08
Python向MySQL批量插數(shù)據(jù)的實(shí)例講解
下面小編就為大家分享一篇Python向MySQL批量插數(shù)據(jù)的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03

