Python学习笔记:网络数据获取
2018-03-09 15:29
447 查看
网络数据获取(爬取)分为两部分:
1、抓取(抓取网页)
· urlib内建模块,特别是urlib.request
· Requests第三方库(中小型网络爬虫的开发)
· Scrapy框架(大型网络爬虫的开发)
2、解析(解析网页内容)
· BeautifulSoup库
· re模块(正则表达式)
或者第三方API抓取和解析。
Requests库(http://www.python-requests.org/en/master/)
基本方法:
requests.get():请求获取指定URL位置的资源,对应HTTP协议中的GET方法。import requests
r=requests.get('https://book.douban.com/subject/1084336/')
r.status_code
Out[3]: 200
r.text
BeautifulSoup库(https://www.crummy.com/software/BeautifulSoup/bs4/doc/)from bs4 import BeautifulSoup
markup = '<p class="title"><b>The Little Prince</b></p>'
soup = BeautifulSoup(markup, "lxml")
soup.b
Out[5]: <b>The Little Prince</b>
type(soup.b)
Out[6]: bs4.element.Tag
tag=soup.p
tag.name
Out[8]: 'p'
tag.attrs
Out[9]: {'class': ['title']}
tag['class']
Out[10]: ['title']
tag.string
Out[11]: 'The Little Prince'
type(tag.string)
Out[12]: bs4.element.NavigableString
soup.find_all('b')
Out[13]: [<b>The Little Prince</b>]
1、抓取(抓取网页)
· urlib内建模块,特别是urlib.request
· Requests第三方库(中小型网络爬虫的开发)
· Scrapy框架(大型网络爬虫的开发)
2、解析(解析网页内容)
· BeautifulSoup库
· re模块(正则表达式)
或者第三方API抓取和解析。
Requests库(http://www.python-requests.org/en/master/)
基本方法:
requests.get():请求获取指定URL位置的资源,对应HTTP协议中的GET方法。import requests
r=requests.get('https://book.douban.com/subject/1084336/')
r.status_code
Out[3]: 200
r.text
import requests re=requests.get('http://finance.sina.com.cn/realstock/company/sh000001/nc.shtml') print(re.text)
BeautifulSoup库(https://www.crummy.com/software/BeautifulSoup/bs4/doc/)from bs4 import BeautifulSoup
markup = '<p class="title"><b>The Little Prince</b></p>'
soup = BeautifulSoup(markup, "lxml")
soup.b
Out[5]: <b>The Little Prince</b>
type(soup.b)
Out[6]: bs4.element.Tag
tag=soup.p
tag.name
Out[8]: 'p'
tag.attrs
Out[9]: {'class': ['title']}
tag['class']
Out[10]: ['title']
tag.string
Out[11]: 'The Little Prince'
type(tag.string)
Out[12]: bs4.element.NavigableString
soup.find_all('b')
Out[13]: [<b>The Little Prince</b>]
import requests from bs4 import BeautifulSoup r=requests.get('https://book.douban.com/subject/1084336/') soup=BeautifulSoup(r.text,'lxml') pattern=soup.find_all('p','comment-content') for item in pattern: print(item.string)
相关文章推荐
- Python学习笔记:便捷数据获取
- Python玩转数据分析学习笔记-03数据获取
- python网络数据采集学习笔记-前言
- Python学习笔记:本地数据获取
- python3.4学习笔记(十四) 网络爬虫实例代码,抓取新浪爱彩双色球开奖数据实例
- python网络数据采集学习笔记:第二章
- 读Python获取网络数据的笔记(9月16日)
- Python网络数据采集学习笔记1
- 『python学习』python 网络数据采集笔记
- WF实例学习笔记:(2)通过Workflow 调用 WCF Data Services 获取数据
- Java学习笔记之网络编程基础-通过URL获取网络图片
- Silverlight学习笔记二:Silverlight从WCF那里获取数据,WCF为Silverlight提供数据
- 黑马程序员之ADO.NET学习笔记:网络数据的事务处理
- Python学习笔记(1)--数据结构
- SilverLight商业应用程序开发---学习笔记(5)从服务器中获取数据之一
- Windows Phone 开发学习笔记(九) RSS阅读器之获取数据
- Java 学习笔记10:获取POST数据以及简单模板输出
- 微软企业库5.0学习笔记(三十四)数据访问模块 各种获取数据的方式
- Java学习笔记之网络编程基础-通过URL获取HTML页面
- SilverLight商业应用程序开发---学习笔记(6)从服务器中获取数据之二