1. 程式人生 > >判斷網頁能否打開?python

判斷網頁能否打開?python

chrom == \ufeff users os x 斷網 python err top

#!/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