首页 > 编程语言 > 详细

判断网页能否打开?python

时间:2017-10-15 16:05:26      阅读:297      评论:0      收藏:0      [点我收藏+]
#!/usr/bin/env python
# coding=utf-8
import requests

headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"}

with open(‘/Users/fin4nc/Desktop/123.txt‘, ‘r‘) as f:
for line in f:
line = line.strip(‘\n‘).strip(‘\ufeff‘)
url = ‘http://‘ + line
print(url)
try:
response= requests.get(url, headers=headers, timeout=5).status_code
print(response)
if response== 200:
print(‘可打开‘)
else:
print(response)
except Exception as e:
with open(‘/Users/fin4nc/Desktop/1.txt‘, ‘a+‘) as f1:
error = f1.write(line+‘\n‘)
print(e)

判断网页能否打开?python

原文:http://www.cnblogs.com/b1ancheng/p/7671181.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!