钓鱼网站规则提取工作的python代码重写总结
2017-05-16 17:07
375 查看
0_originJson_writeURL_inTxt.py:
import urllib.request import json origin_json_data = urllib.request.urlopen(r"http://www.txwz.qq.com/lib/index.php?m=enterprise&a=get_exsample").read() ndata = json.loads(origin_json_data) # ndata is decode_json_data file_name = "f:/2017-05-16.txt" # "/" "\" 或不写 都可以 #这个是你放网址的文件名,改过来就可以了 file_open = open(file_name, "a") # 把提取出的信息写入file_name.txt文件 for i in range( 0,len(ndata["data"]) ): type_data = ndata["data"][i]["bn"] url_data = ndata["data"][i]["n"] #从解码后的json数据中提取type、url信息 if "http" not in url_data: url_data = "http://" + url_data #将前缀没有http;//的url加上协议前缀 file_open.write(url_data + "\n") #将已经提取的信息写入txt文件 file_open.close() #写入完毕 print("txt_write finish")
1_txt_openableUrl_saveInTxt.py:
import urllib.request import time import requests #抓取并保存网页源码要用的包 opener = urllib.request.build_opener() opener.addheaders = [('User-agent', 'Mozilla/49.0.2')] file = open('f:/1.txt') #这个是你想检测能否打开的url所在文件名,改过来就可以了 lines = file.readlines() aa=[] for line in lines: temp=line.replace('\n','') aa.append(temp) print(aa) print('开始检查:') count = 0 # 计算txt中能打开的网站的数量 #newfile = open("f:/URL_open.txt","a") #这个是你保存能打开网址的文件名,改过来就可以了 for a in aa: tempUrl = a try : opener.open(tempUrl) print(tempUrl+'没问题') newfile = open("f:/URL_open.txt","a") newfile.write(tempUrl+"\n") #将能打开的url写入f:/URL_open.txt newfile.close() count = count + 1 html = requests.get(tempUrl) tempfileName = "f:/" + str(count) + "_" + str(tempUrl[7:]) + ".txt" f = open(tempfileName,'a',encoding='utf-8') f.write(html.text) f.close() #将能打开的url写入f:/URL_open.txt后 并且抓取其网页源码并保存至f:/count_url.txt中count、url都随之变化 except urllib.error.HTTPError: print(tempUrl+'=访问页面出错') time.sleep(2) except urllib.error.URLError: print(tempUrl+'=访问页面出错') time.sleep(2) time.sleep(0.1) #newfile.close() print("txt_openableUrl_saveInTxt-------->finish")
相关文章推荐
- python 提取主域名和子域名代码——先根据规则提取,如果有问题,则使用tldextract
- access中[身份证号]字段有效性规则的代码实现,以及自动从[身份证号]提取出生日期和性别
- ActiveReports工作总结8——用代码控制布局
- 工作中处理文本的python代码片段
- SIFT特征提取代码运行总结
- 特征提取代码总结
- 在即将离开工作了四年的软件业前[zt,一位前辈关于代码优化的总结]
- 代码走查工具篇SytleCop的规则总结与翻译
- 特征提取代码总结
- android jni代码编写规则--整理总结
- 使用python提取html文件中的特定数据的实现代码
- 项目总结-代码之前的工作
- 11月29日 工作总结(ASP.NET代码收藏)
- 11月29日 工作总结(ASP.NET代码收藏)
- android jni代码编写规则--整理总结
- ActiveReports工作总结8——用代码控制布局
- 11月29日 工作总结(ASP.NET代码收藏)
- 2010工作代码总结之一(XtrGrid)
- 工作总结之源代码目录结构
- 使用python提取html文件中的特定数据的实现代码