首页 > 编程语言 > 详细

Python写的网络爬虫程序(很简单)

时间:2014-11-27 22:06:35      阅读:290      评论:0      收藏:0      [点我收藏+]

Python写的网络爬虫程序(很简单)

这是我的一位同学传给我的一个小的网页爬虫程序,觉得挺有意思的,和大家分享一下。不过有一点需要注意,要用python2.3,如果用python3.4会有些问题出现。


python程序如下:

import re,urllib
strTxt=""
x=1
ff=open("wangzhi.txt","r")

for line in ff.readlines():
	f=open(str(x)+".txt","w+")
	print line
	n=re.findall(r"<p>(.*?)<\/p>",urllib.urlopen(line).read(),re.M)
	for i in n:
		if len(i)!=0:
			i=i.replace(" ","")
			i= i.replace("<strong>","")
                        i = i.replace("</strong>","")
                        strTxt = strTxt + i
                        strTxt = re.sub(r"<a href=(.*?)>", r"", strTxt)
                        strTxt=re.sub(r"<a(.*?)>",r"",strTxt)
                        strTxt=re.sub(r"<span>(.*?)</span>",r"", strTxt)
                        strTxt = re.sub(r"<\/[Aa]>", r"", strTxt)
                #print strTxt
                f.write(strTxt)
                strTxt=""
        f.close
        x=x+1
ff.close()
</span>


wangzhi.txt的内容如下:

http://sports.163.com/14/1126/22/AC0TVK4E00052UUC.html
http://sports.163.com/14/1126/22/AC0TGD4700052UUC.html
http://sports.163.com/14/1126/22/AC0TAHNK00052UUC.html


结果分析:

运行程序,有3个输出文件,分别是3个URL地址对应的网页的内容。

Python写的网络爬虫程序(很简单)

原文:http://blog.csdn.net/sxhlovehmm/article/details/41553705

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!