分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

web crawling(plus5) news crawling and proxy

发布时间:2023-09-06 01:15责任编辑:苏小强关键词:暂无标签
#Author:Mini
#!/usr/bin/env python
import urllib.request
import urllib.error
import re
data=urllib.request.urlopen("http://news.sina.com.cn/").read()
data1=data.decode("utf-8","ignore")
pat=‘ href="(http://news.sina.com.cn/.*?)">‘
allurl=re.compile(pat).findall(data1)
for i in range(0,len(allurl)):
???try:
???????print(str(i)+"\n\ntime")
???????thisurl=allurl[i]
???????fh="E:/m/"+str(i)+".html"
???????urllib.request.urlretrieve(thisurl,fh)
???????print("success!")
???except urllib.error.URLError as e:
???????if hasattr(e, "code"):
???????????print(e.code)
???????????if hasattr(e, "reason"):
???????????????print(e.reason)


************************************
import urllib.request
import re
import ?urllib.error

def use_proxy(url,proxy_addr):
???proxy=urllib.request.ProxyHandler({"http":proxy_addr})
???opener1=urllib.request.build_opener(proxy,urllib.request.HTTPHandler)
???urllib.request.install_opener(opener1)
proxy_addr="220.161.37.21:8118"
url="http://blog.csdn.net/"
headers=("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36Query String Parametersview sourceview URL encoded")
opener=urllib.request.build_opener()
opener.addheaders=[headers]
urllib.request.install_opener(opener)
data=use_proxy(url,proxy_addr)
data=urllib.request.urlopen(url).read().decode("utf-8","ignore")
print(len(data))

pat=‘ <h3 ?class="csdn-tracking-statistics" data-mod="popu_430" data-poputype="feed" ?data-feed-show="false" ?data-dsm="post"><a href="(.*?)"‘
res=re.compile(pat).findall(data)
for i in range(0,len(res)):
try:
???fil="E:/m/"+str(i)+".html"
???urllib.request.urlretrieve(res,filename=fil)
???print(str(i),"\n\ntime")
except urllib.error.URLError as e:
???if hasattr(e, "code"):
???????print(e.code)
???????if hasattr(e, "reason"):
???????????print(e.reason)

web crawling(plus5) news crawling and proxy

原文地址:http://www.cnblogs.com/rabbittail/p/7623041.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved