python多執行緒採集例項

2021-06-22 02:31:43 字數 1617 閱讀 7269

python多執行緒採集例項

python作為一種流行的指令碼程式語言,其功能的強大自然不言而喻,豆瓣網就是使用python開發的,另外很多的it公司了在使用python,也充分說明了python的強大,下面是python多執行緒採集例項**。

python多執行緒採集例項**:

#coding=gb2312

#author: [email protected] 因為分享我們變得更好!

import re

import urllib

import time

import threading

#程序鎖

lock = threading.lock()

#存放記錄條數

count =0

#存放url資訊

url=

#第一次正則匹配搜尋結果

rc = re.compile(r'()(.*)[0-9]-[0-9]-[0-9]',re.i)

#第二次正則匹配搜尋結果

rcr = re.compile(r'^>(.*)(/ )?(\s)?',re.i)

#替換重複的搜尋結果

#多執行緒搜尋

class seorearch(threading.thread):

def __init__(self,wd='dedecms',n=0):

self.wd = wd

self.n = n

super(seorearch,self).__init__()

def run(self):

global count

file = urllib.urlopen(""+str(self.n)+"&q1="+self.wd).read()

for i in rc.finditer(file):

f=i.group(0)

temp = f.replace('','').replace('','')

time.sleep(1)

t = temp[16:temp.find('/')]

print t

lock.acquire()

count += 1

lock.release()

if __name__== '__main__':

s = raw_input("請輸入你要查詢的關鍵字")

page = raw_input("請輸入要查詢的頁數每頁50條")

file1 = open('record.txt','w')

threads=

for i in range(int(page)):

t = seorearch(wd=s,n=i*50)

t.start()

for t2 in threads:

t2.join()

list_insteah = list(set(url))

time.sleep(2)

for i in list_insteah:

file1.write(i+"\n")

file1.close()

print "你搜尋的關鍵字是:%s,搜尋的頁數是:%s,總計一共 %s 條資訊" % (s,page,count)

over!大家可以試試。

python多執行緒例項

a 建立乙個thread的例項,傳給他乙個函式 import threading from time import sleep def thread body arg1,arg2 print i am a child thread arg1,arg2 sleep 10 print end child...

python多執行緒例項

import threading import time class mythread threading.thread def init self,n super mythread,self init self.n n def run self print runnint task self.n ...

python多執行緒 採集ip並驗證

python因gil的設計使得單cpu同一時刻只能執行乙個執行緒,所以多執行緒在python中主要用於io較多的場景。前段時間需要採集一些免費ip 就應用多執行緒對採集來的 進行目標 的連通性進行批量驗證。python通過兩個標準庫thread和threading提供對執行緒的支援。thread提供...