python使用BeautifulSoup解析html获得网站的百度收录量,,BeautifulSou
python使用BeautifulSoup解析html获得网站的百度收录量,,BeautifulSou
BeautifulSoup解析html非常方便,主要使用它的find()
和findAll()
方法来找到页面上的指定元素。
安装BeautifulSoup
在命令行使用如下指令安装:
easy_install BeautifulSoup
使用BeatifulSoup
我们已获得网站的baidu收录数为例,如下python代码:
# -*- coding: cp936 -*-import urllibfrom BeautifulSoup import BeautifulSoupimport redef get_baidu_records_count(host): url = 'http://www.baidu.com/s?wd=site%3A' + host data = urllib.urlopen(url) html = data.read() soup = BeautifulSoup(html) #使用find方法找到class为site_tip的p标签 siteTipP = soup.find('p',{'class':'site_tip'}) if not siteTipP: return 0 #找到p标签的第一个strong标签 strong = siteTipP.find('strong') #使用.string获得strong标签的内容 text = strong.string numPattern = re.compile(r'\d+') m = numPattern.search(text) strCn = m.group(0) return int(strCn)if __name__ == '__main__': host = 'OutOfMemory.CN' print '%s的百度收录量为%d' % (host,get_baidu_records_count(host))
运行程序可以获得OutOfMemory.CN的收录量,可惜现在百度收录量很差!还得继续加油!
相关内容
- 使用python对png图片文件做base64编码,,有时候我们需要使
- python根据进程名杀死进程,python杀死进程,在linux/unix平
- Python @staticmethod 和 @classmethod之间的区别,,使用@static
- Python 数组分隔总结,python数组分隔,初学Python被它的数
- 在python中调用外部命令,python调用外部命令,在python中有
- Python内存调优,python调优,在python中可以使用
- 查询关键词在百度排名python脚本分享,python脚本,如下脚
- python 的IO文件操作总结,,在项目开发过程中,时常需
- Python如何查看变量占用空间大小,python变量占用空间
- error: 2006 MySQL server has gone away 解决方法,mysqlgone,今天在
评论关闭