Python 简单的爬取网站状态码标题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import requests
import re
import time
import threadpool
import traceback
code_200 = []
code_not200 = []
_REGEX_ = '<title>(.*)</title>'
def getTitle(url):
try:
response = requests.get(url)
pattern = re.compile(_REGEX_, flags=re.IGNORECASE)
try:
title = re.findall(pattern, response.text)[0]
except IndexError:
title = '无标题'
if response.status_code == 200:
code_200.append({'url':url,'title':title,"status_code":response.status_code})
print({'url':url,'title':title,"status_code":response.status_code})
return
else:
code_not200.append({'url':url,'title':title,"status_code":response.status_code})
print({'url': url, 'title': title, "status_code": response.status_code})
return
except Exception as e:
traceback.print_exc()
return 0


if __name__ == '__main__':
domain = []
with open('test5.txt','r') as f:
for i in f.readlines():
domain.append(i.strip())
f.close()
print(domain)
start_time = time.time()
pool = threadpool.ThreadPool(100)
requests_1 = threadpool.makeRequests(getTitle, domain)
[pool.putRequest(req) for req in requests_1]
pool.wait()
with open('test6.txt','w+',encoding='UTF-8') as f:
for i in code_200:
f.write(str(i) + '\n')
for i in code_not200:
f.write(str(i) + '\n')
print('%d second' % (time.time() - start_time))
-------------本文结束感谢您的阅读-------------