python获取并正则匹配html中的内容

#coding=utf-8
import urllib
import re
def getHtml(url):
    page = urllib.urlopen(url)
    html = page.read()
    return html

def getlink(html):
    reg = raw_input('Please input Regular Expression:')
    linkre = re.compile(reg)
    linklist = re.findall(linkre,html)
    return linklist
	
address = raw_input('Please input url http://')
html = getHtml('http://'+address)
res = getlink(html)

newfile = file('Result.txt','w')
for i in res:                   
    newfile.write(i+'n')
    print i
newfile.close()
print 'Find',len(res),'nOutput file : Result.txt'

 

发表评论

Fill in your details below or click an icon to log in:

WordPress.com 徽标

You are commenting using your WordPress.com account. Log Out /  更改 )

Google photo

You are commenting using your Google account. Log Out /  更改 )

Twitter picture

You are commenting using your Twitter account. Log Out /  更改 )

Facebook photo

You are commenting using your Facebook account. Log Out /  更改 )

Connecting to %s