第一章 初见网络爬虫
来源:互联网 发布:网络lw代表什么意思 编辑:程序博客网 时间:2024/06/05 20:35
urllib是Python的标准库,包含了从网络请求数据,处理Cookie,甚…..本书中广泛使用urllib,所以建议读读这个库的Python文档
urllib官方文档 廖雪峰教程
from urllib.request import urlopenhtml = urlopen("http://www.pythonscraping.com/pages/page1.html");print(html.read());
BeautifulSoup:它通过定位HTML标签来格式化和组织复杂的网络信息。
安装Python的包管理器pip,然后cmd在命令提示符里运行:
pip install beautifulsoup4
运行BeautifulSoup
from urllib.request import urlopenfrom bs4 import BeautifulSouphtml = urlopen("http://www.pythonscraping.com/pages/page1.html");bs0bj = BeautifulSoup(html.read());print(bs0bj.h1)
可能的异常处理:
from urllib.request import urlopenfrom urllib.error import HTTPError,URLErrorfrom bs4 import BeautifulSoupdef getTitle(url): try: html=urlopen(url); except(HTTPError,URLError) as e: return None; try: bs0bj = BeautifulSoup(html.read()) title = bs0bj.body.h1; except AttributeError as e: return None; return title;title=getTitle("http://www.pythonscraping.com/pages/page1.html");if title == None: print("Title could not be found");else: print(title);
写爬虫的时候,思考代码的总体格局,让代码可以捕捉异常又容易阅读。
0 0
- 第一章 初见网络爬虫
- 初见Python网络爬虫
- 初见网络爬虫
- 第1章初见网络爬虫
- 用python写网络爬虫读书笔记 第一章网络爬虫简介
- 第一章:初见opencore
- T100 第一章 初见T100
- 《用python写网络爬虫》第一章
- 【Python网络爬虫】python网络数据采集读书笔记(第一章)
- 读书笔记——自己动手写网络爬虫_第一章(1)
- 读书笔记——自己动手写网络爬虫_第一章(2)
- 第一章 人生若只如初见,何事秋风悲画扇
- 面试之网络编程初见
- 初见SpringMVC之网络请求
- 爬虫第一章笔记
- 第一章 创建爬虫常用命令
- 初见
- 初见
- MySQL学习笔记
- nachos-java Task1.6 Boat Program
- UIButton上的文字产生刷新动画
- sql查询优化总结ing
- office熟练是怎么一回事?
- 第一章 初见网络爬虫
- linux主机开机过程
- Json(三)之php操作json
- linux下文件的access time 、change time、modify time
- [BZOJ2628][压位][Shift-And&Shift-Or]JZPSTR
- 二叉树遍历
- 10CloudNote
- 欢迎使用CSDN-markdown编辑器
- C++STL模板笔记(一)