1)②爬取光明网部分旅游新闻

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

1__author__='minmin'2#coding:utf-83importre,urllib,sgmllib45#根据当前的url获取html6defgetHtml(url):7page =urllib.urlopen(url)8html =page.read()9page.close()10returnhtml1112#根据html获取想要的文章内容13deffunc(str):14result= re.findall(r"<p style=\"TEXT-INDENT: 30px; MARGIN: 0px 3px 15px\">([^<>]*)</p>",getHtml(url),re.M)orre.findall(r"<p>([^<>]*)</p>",getHtml(url),re.M)15#or re.findall( r"<p style=\"TEXT-JUSTIFY: distribute; TEXT-ALIGN: justify\" align=\"justify\">(.*?)</p>",getHtml(url),re.M)16artical =''17forjinresult:18iflen(j)<>0:19j = j.replace("&nbsp;","")20j = j.replace("<strong>","")#去掉<STRONG>,换成" "21j = j.replace("</strong>","")#去掉</STROGN>换成" "22artical = artical + j +'\n'23returnartical2425#html链接的标签是“a”,链接的属性是“href”,也就是要获得html中所有tag=a,attrs=href 值。26classURLPaser(sgmllib.SGMLParser):27defreset(blank):28sgmllib.SGMLParser.reset(blank)29blank.urls =[]3031defstart_a(blank,attrs):32href = [vfork,vinattrsifk =='href']33ifhref:34blank.urls.extend(href)3536iparser =URLPaser()37socket = urllib.urlopen("http://travel.gmw.cn/node_39034.htm")#打开这个网页3839#fout = file('QQ_art_urls.txt','w')#要把这个链接写到这个文件中40IParser.feed(socket.read())#分析啦4142reg ='http://travel.gmw.cn/2015-.*'#这个是用来匹配符合条件的链接,使用正则表达式匹配43reg2='http://travel.gmw.cn/2014-.*'44pattern =re.compile(reg)45patter =re.compile(reg2)46i=047url2=[]48forurlinIParser.urls:#链接都存在urls里49url ="http://travel.gmw.cn/"+url50ifpattern.match(url):51ifurlnotinurl2:52url2.append(url)53PRinturl54artical =func(url)55printartical56iflen(artical)<>0:57i = i + 158f = open("gmw/travel/"+str(i) +'.txt','a+')59f.write(artical)60f.close()6162ifpatter.match(url):63ifurlnotinurl2:64url2.append(url)65printurl66printartical67iflen(artical)<>0:68i = i + 169f = open("gmw/travel/"+str(i) +'.txt','a+')70f.write(artical)71f.close()

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航