利用正则表达式编写python 爬虫,抓取网页联系我们电话号码!这里以九奥科技(www.jiuaoo.com)为例,抓取‘联系我们’里面的电话号码,并输出。
#!/usrweilie/bin/python
#coding=utf-8
import re
import urllib
def gethtml(url): #获取网页html
jiuaoopage=urllib.urlopen(url)
html=jiuaoopage.read()
return html
def getstr(html): #利用正则表达式抓取‘联系我们’链接
r=r'联系我们' #正则表达式
fo=re.compile(r)
str1=fo.findall(html)
return str1
def gettel(html): #利用正则表达式抓取电话号码
r=r'\d{3}\-\d{8}'
fo=re.compile(r)
tel=fo.findall(html)
print tel
html=gethtml('http://www.jiuaoo.com/')
str1=getstr(html)
str2='http://www.jiuaoo.com'
for i in str1:
urlstr=str2+i
html=gethtml(urlstr)
gettel(html)
一个抓取电话号码的小爬虫。