python多執行緒爬蟲

2021-10-22 09:17:42 字數 1992 閱讀 8788

blog_spider.py

# ********************==

# coding: utf-8

# author:mr. luo

# date:2021/3/16 13:57

# ********************===

import requests

import csv

urls = [f"" for page in range(1,6)]

def craw(url):

r = requests.get(url)

print(url, len(r.text))# f = open('./ex3.csv', 'a+', newline='')

# writer = csv.writer(f)

# writer.writerow(str(r.status_code))

# writer.writerow(str(x))

# writer.writerow(r.headers)

# writer.writerow(str(r.url))

# writer.writerow(('li', 'female', '18'))

# f.close()

# # 列印狀態碼

# print(r.status_code)

# # 列印請求url

# print(r.url)

# # 列印頭資訊

# print(r.headers)

# # 列印cookie資訊

# print(r.cookies)

# # 以文字形式列印網頁原始碼

# print(r.text)

# # 以位元組流形式列印

# print(r.content)

# # print(url, len(r.text))

if __name__ == '__main__':

craw(urls[0])

multi_thread.py

# ********************==

# coding: utf-8

# author:mr. luo

# date:2021/3/16 14:00

# ********************===

'''python多執行緒爬蟲

'''import blog_spider

import threading

import time

def single():

for url in blog_spider.urls:

blog_spider.craw(url)

def multi_thread():

threads =

# 建立多執行緒

for url in blog_spider.urls:

# 最後的,保證args為元組形式

threading.thread(target=blog_spider.craw,args=(url,))

)# 啟動多執行緒

for thread in threads:

thread.start()

# 等待結束多執行緒

for thread in threads:

thread.join()

if __name__ == '__main__':

start = time.time()

single()

end = time.time()

print("single thread cost:", end - start)

print("*"*10)

start = time.time()

multi_thread()

end = time.time()

print("multi thread cost:", end - start)

python爬蟲 多執行緒爬蟲

在進行爬蟲工作的時候,考慮到爬蟲執行的速度慢,那麼怎樣提公升爬蟲的速度呢,那麼就得使用多執行緒爬蟲了,接下來我以糗事百科段子的爬取進行對多執行緒爬蟲的概述 github鏈結 鏈結一 不使用多執行緒爬取糗事百科 1.上 import urllib.request import re headers f...

python多執行緒爬蟲

先記錄一下,普通的糗事百科爬蟲 import urllib.request import re import time import urllib.error headers user agent mozilla 5.0 windows nt 10.0 win64 x64 rv 63.0 gecko...

python多執行緒爬蟲

python多執行緒爬蟲 python單執行緒爬蟲對於應付小規模資料是可以的,但是面對大量資料,我們就要用到多執行緒爬蟲技術。使用多執行緒,一方面可能會加快效率,另一方面可以施加一些小技巧,如不同的執行緒使用不同的 ip從而避免出發反爬機制。python 多執行緒 python的多執行緒可以用thr...