# coding=utf-8
import requests
import re
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
url = "http://top.chinaz.com/all/index_br.html"#由于第一页跟第二页之后的url格式不一样,so,分两步完成.
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"}
session = requests.session()
html = session.get(url, headers=headers).content
config = re.compile('(.*?).*?(.*?).*?')
res = re.findall(config, html)
connecterror = []
f = open("url.txt", "a")
print '''程序启动... '''
for i in range(1, len(res)):
if "www" in res[0:3]:
cc = "http://" + res[0]
try:
a= cc + ' | Alexa周排名: ' + res[i - 1][1] + ' | 百度权重: ' + res[i - 1][2] + ' | Server: ' + requests.get(cc, headers=headers).headers['Server']
print a
f.write(a+'\n')
except:
a = cc + ' | Alexa周排名: ' + res[i - 1][1] + ' | 百度权重: ' + res[i - 1][2] + ' | Server: 获取失败'
print a
f.write(a+'\n')
connecterror.append(cc)
else:
cc = "http://www." + res[0]
try:
a = cc + ' | Alexa周排名: ' + res[i - 1][1] + ' | 百度权重: ' + res[i - 1][2] + ' | Server: ' + requests.get(cc, headers=headers).headers['Server']
print a
f.write(a+'\n')
except:
a = cc + ' | Alexa周排名: ' + res[i - 1][1] + ' | 百度权重: ' + res[i - 1][2] + ' | Server: 获取失败'
print a
f.write(a+'\n')
connecterror.append(cc)
for j in range(2,1844):
url = "http://top.chinaz.com/all/index_br_%d.html"%j
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"}
session = requests.session()
html = session.get(url, headers=headers).content
config = re.compile('(.*?).*?(.*?).*?