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(" ","")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()