时间:2021-07-01 10:21:17 帮助过:89人阅读
import urllib.request import http.cookiejar import socket import time import re timeout = 20 socket.setdefaulttimeout(timeout) sleep_download_time = 10 time.sleep(sleep_download_time) def makeMyOpener(head = { 'Connection': 'Keep-Alive', 'Accept': 'text/html, application/xhtml+xml, */*', 'Accept-Language': 'en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko' }): cj = http.cookiejar.CookieJar() opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) header = [] for key, value in head.items(): elem = (key, value) header.append(elem) opener.addheaders = header return opener def saveFile(save_path,txts): f_obj = open(save_path,'w+') for item in txts: f_obj.write(item+'\n') f_obj.close() #get_code_list code_list='http://www.cishuge.com/read/0/771/' oper = makeMyOpener() uop = oper.open(code_list,timeout=1000) data = uop.read().decode('gbk','ignore') pattern = re.compile('
虽然代码还是有点瑕疵,还是分享给大家,一起改进