https://www.runoob.com/python3/python3-multithreading.html
import requests
import threading
import time
import xlrd
import os
def url_get():
http_url = []
data = xlrd.open_workbook(‘D:\Python培训\shenzhou\其他\URL.xlsx’)
table = data.sheets()[0]
for i in range(0, 10):
http_url.append(table.cell(i, 0).value)
print(http_url)
proxy = {'http': 'http://10.121.51.142:808'}
header = {'Proxy-Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Cookie': 'pgv_pvi=9344595968; pt2gguin=o0695374133; RK=nCZg90QwYB; ptcz=a81ce6404c6375a8a014f4d9b6d5f5c1a45a13b1b7b3c93ae1c1d385e4e92da3; pgv_pvid=5582762548; o_cookie=695374133; ts_uid=2383234090; pac_uid=1_695374133; luin=o0695374133; lskey=0001000092d5b09b14c96c263e80a80f83ba04327b2ab003ff213eac669110ee50306a641fbfb66f9cc324b1; pgv_info=ssid=s7603536448; ts_last=www.qq.com/; ad_play_index=37; qv_als=JE9gAQhFjh47CMsnA11534567765hdXuQA==; ptcz=a81ce6404c6375a8a014f4d9b6d5f5c1a45a13b1b7b3c93ae1c1d385e4e92da3; fp3_id1=11005E19CE6759F6CDD82D1A36E8E1ABB05A328ACEBEF88B945C64135723355A19BA3FBB73EEF95256E74FCA08A912FA082E'}
# url = ['http://www.qq.com','http://www.youdao.com']
j=1
for i in http_url:
try:
requests.get(i)
# time.sleep(1)
print('success:request URL %d:%s' % (j, i))
j = j + 1
except Exception as e:
# print(e)
print('error:request URL %d:%s' % (j, i))
j = j + 1
class myThread (threading.Thread):
def init(self, name):
threading.Thread.init(self)
self.name = name
def run(self):
print(“开始线程:” + self.name)
#通过父进程和子进程可以看出所有线程都归属在同一个进程下
print(‘parent process:’, os.getppid())
print(‘process id:’, os.getpid())
url_get()
print(“退出线程:” + self.name)
thread_list=[]
for i in range(0,5):
name=‘thread’+str(i)
print(name)
name=myThread(name)
thread_list.append(name)
for thread in thread_list:
thread.start()
#thread.join() // 如果启用,线程是逐个执行的
run(): 用以表示线程活动的方法。
start():启动线程活动。
join([time]): 等待至线程中止。这阻塞调用线程直至线程的join() 方法被调用中止-正常退出或者抛出未处理的异常-或者是可选的超时发生。
isAlive(): 返回线程是否活动的。
getName(): 返回线程名。
setName(): 设置线程名。