Web Scraping with Python-Chapter1读书笔记
来源:互联网 发布:蒋方舟东京一年 知乎 编辑:程序博客网 时间:2024/06/06 01:51
前记:正式开始我的Python爬虫之旅
Chapter 1. Your First Web Scraper
1.库函数的安装
本章涉及两个库函数的使用,分别是urllib与BeautifulSoup 4 library(通常也被称为BS4)。前者是Python的标准库,BS4需要自行安装。WIN10系统的安装方法:执行命令pip install beautifulsoup4。过程如下:
D:\PythonProject\webScraping>pip install beautifulsoup4Collecting beautifulsoup4 Downloading beautifulsoup4-4.5.1-py3-none-any.whl (83kB) 100% |████████████████████████████████| 92kB 67kB/sInstalling collected packages: beautifulsoup4Successfully installed beautifulsoup4-4.5.1D:\PythonProject\webScraping>
2.网页爬取例子
from urllib.request import urlopenfrom urllib.error import HTTPErrorfrom bs4 import BeautifulSoupdef getTitle(url): try: html = urlopen(url) except HTTPError as e: return None try: bsObj = BeautifulSoup(html.read()) title = bsObj.body.h1 except AttributeError as e: return None return titletitle = getTitle("http://www.pythonscraping.com/exercises/exercise1.html")# bsObj = BeautifulSoup(html.read())# print(bsObj.h1)if title == None: print("Title not found")else: print(title)
3.程序的运行结果
a.exercise1.html网页的源码如下
<html><head><title>A Useful Page</title></head><body><h1>An Interesting Title</h1><div>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div></body></html>
b.程序的爬取结果如下<h1>An Interesting Title</h1>Process finished with exit code 0
4.异常处理说明
html = urlopen(url)
urlopen()函数会涉及两种错误:
1.在服务器上没有找到访问的url页
2.访问的服务器不存在
两种错误的处理方式如下:
第一种,返回HTTP错误:“404 PageNot Found,” “500 Internal Server Error,”等。urlopen()函数会抛出“HTTPError”
第二种,urlopen()函数会返回None
另外写爬虫程序需要考虑到代码处理异常与可读性的平衡
0 0
- Web Scraping with Python-Chapter1读书笔记
- 《Web Scraping with Python》读书笔记
- Web Scraping with Python 读书笔记
- Web Scraping with Python 学习笔记6
- Web Scraping with Python 学习笔记7
- Web Scraping with Python 学习笔记8
- Web Scraping with Python 学习笔记9
- Web Scraping with Python 学习爬虫
- 首部讲Python爬虫电子书 Web Scraping with Python
- Python网络爬虫<Web Scraping With Python-----O‘Reilly>
- Web Scraping with Python: 使用 Python 爬 GitHub Star 数
- Web Scraping with Python: 使用 Python 爬 CSDN 博客
- Web Scraping with Python: 使用 Python 爬 Baidu 关键词
- Web Scraping with Python: 使用 Python 下载 CSDN 博客图片
- 使用API 第四章web scraping with python
- 第五章 存储数据 web scraping with python
- web scraping with python 第六章、读取文档
- [翻译]<Web Scraping with Python>Chapter 0.前言
- HTML5+CSS3整体回顾
- Spring Security教程(三)
- 一个好的目标应满足的原则(SMART)
- 如何根据接口写一个客户端进行发单测试?
- 关于“异或”的很好的解释
- Web Scraping with Python-Chapter1读书笔记
- 为什么GridLayout下面会多出一片空白区域?并且还遮盖了我的一个视图
- 新手求助哇!为什么我用intent传值会报错~
- React Native实战项目企业通信录(含视频教程)- 登录界面开发
- 在AndroidStudio中使用SVN
- IOS网络编程发送数据的问题
- actionbar图标变黑
- java环境搭建
- Ubuntu 14.04中安装insight debugger