Python3 BeautifulSoup4结合urllib简单使用
2013-12-15 15:00
429 查看
1、使用urllib下载网页
2、构造BeautifulSoup对象
3、剖析文档
代码示例:【需要注意import模块版本的区别】
输出结果:
2、构造BeautifulSoup对象
3、剖析文档
代码示例:【需要注意import模块版本的区别】
import urllib.request from bs4 import BeautifulSoup import re def getgxnu(): url="http://www.gxnu.edu.cn/default.html" data=urllib.request.urlopen(url).read() page_data=data.decode('GBK') '''print(page_data)''' soup=BeautifulSoup(page_data) #for link in soup.findAll('a',target='_self'):#get all links of gxnu index # print(link) for link in soup.findAll('a',href=re.compile('http://\\S+/type/\\d+.html')):#使用正则表达式 print(link['href'],link.contents) #函数调用 getgxnu()
输出结果:
>>> http://www.gxnu.edu.cn/type/010400000102.html ['\n', <span>学校概况</span>] http://www.gxnu.edu.cn//type/010400000102.html ['学校简介'] http://www.gxnu.edu.cn/type/01040000010202.html ['学校沿革'] http://www.gxnu.edu.cn/type/01040000010203.html ['学校领导'] http://www.gxnu.edu.cn/type/01040000010204.html ['校园风光'] http://www.gxnu.edu.cn/type/01040000010205.html ['校园地图'] http://www.gxnu.edu.cn/type/01040000010206.html ['校歌 校训 校徽'] http://www.gxnu.edu.cn/type/01040000010207.html ['学校视觉形象识别系统'] http://www.gxnu.edu.cn/type/01040000010208.html ['校史资料'] http://www.gxnu.edu.cn/type/010400000103.html ['\n', <span>机构设置</span>] http://www.gxnu.edu.cn/type/010400000103.html ['教学单位'] http://www.gxnu.edu.cn/type/010400000103.html ['管理部门'] http://www.gxnu.edu.cn/type/010400000103.html ['业务部门'] http://www.gxnu.edu.cn/type/010400000103.html ['附属单位'] http://www.gxnu.edu.cn/type/010400000104.html ['\n', <span>学术研究</span>] http://www.gxnu.edu.cn/type/01040000010401.html ['科研管理'] http://www.gxnu.edu.cn/type/01040000010402.html ['科研机构'] http://www.gxnu.edu.cn/type/01040000010403.html ['重点学科'] http://www.gxnu.edu.cn/type/01040000010404.html ['重点实验室'] http://www.gxnu.edu.cn/type/01040000010405.html ['学术刊物'] http://www.gxnu.edu.cn/type/010400000105.html ['\n', <span>人才队伍</span>] http://www.gxnu.edu.cn/type/010400000106.html ['\n', <span>招生就业</span>] http://www.gxnu.edu.cn/type/01040000010601.html ['招生信息'] http://www.gxnu.edu.cn/type/01040000010602.html ['就业信息'] http://www.gxnu.edu.cn/type/01040000010603.html ['就业指导'] http://www.gxnu.edu.cn/type/010400000107.html ['\n', <span>图书档案</span>] http://www.gxnu.edu.cn/type/01040000010801.html ['\n', <span>公共服务</span>] http://www.gxnu.edu.cn/type/01040000010801.html ['教学服务'] http://www.gxnu.edu.cn/type/01040000010805.html ['其他'] http://www.gxnu.edu.cn/type/010400000111.html ['\n', <span>校园文化</span>] http://www.gxnu.edu.cn/type/01040000011101.html ['学生社团'] http://www.gxnu.edu.cn/type/010400000112.html ['\n', <span>ENGLISH</span>] >>>
相关文章推荐
- Python之安装pip&beautiful4&使用beautifulsoup4抓取简单网页
- 使用python语言结合beautifulsoup编写简单的网络爬虫
- Python urllib简单使用
- 在python3中使用urllib.request编写简单的网络爬虫
- Python中使用urllib2模块编写爬虫的简单上手示例
- Python中使用urllib2模块编写爬虫的简单上手示例
- 在python3中使用urllib.request编写简单的网络爬虫
- 在python3中使用urllib.request编写简单的网络爬虫
- Python模块之urllib和urllib2结合使用
- python爬虫提取数据之Beautifulsoup4简单使用
- Python爬虫之urllib简单使用
- python2.7 httplib, urllib, urllib2, requests 库的简单使用
- urllib库的简单使用 && 一个简单的Python爬虫示例
- python爬虫提取数据之Beautifulsoup4简单使用
- Windows下Elasticsearch环境搭建并结合Python简单使用
- python2.7 httplib, urllib, urllib2, requests 库的简单使用
- Python 标准库 urllib2 的使用细节
- Python3.7 爬虫(一)使用 Urllib2 与正则表达式抓取
- 浅谈python字符串方法的简单使用
- cocos2d-x 使用 python脚本 创建项目的简单方法