Python开发笔记:网络数据抓取
2023-09-14 08:57:35 时间
网络数据获取(爬取)分为两部分:
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列表到字符串的转换[通俗易懂]
- python进制转换函数-Python中进制转换函数的使用
- python整除和取余写法_Python的整除和取余[通俗易懂]
- python中sqrt函数用法_Python : sqrt() 函数
- python hashlib_Python hashlib模块实例使用详解
- 符合python命名规范的标识符是什么_Python标识符命名规范
- Python海龟画图集合
- python海龟绘图画圆_Python启蒙之海龟作图「建议收藏」
- python基础系列教程——Python的安装与测试:python解释器、PyDev编辑器、pycharm编译器
- python语言一般用于什么_PYthon
- Python: Requests库网络爬取实战
- Python控制手机_能控制玩手机的软件
- Python 技巧篇-pip卸载python库实例演示,查看pip命令大全方法[通俗易懂]
- python编程 input输入函数
- 【说站】python判断变量的方法对比
- python 类属性和实例属性、类方法, 静态方法, 实例方法、接口, 协议和抽象基类 (4.2)
- 【Python深度学习之路】-2.2 过拟合与集成学习
- Python 反转字符串_python输出字符串
- python deepcopy函数_Python deepcopy
- 【7】python_matplotlib 输出(保存)矢量图方法;画图时图例说明(legend)放到图像外侧;Python_matplotlib图例放在外侧保存时显示不完整问题解决
- 【错误记录】Mac 中 IntelliJ IDEA 运行 Python 程序报错 ( pip 21.0 will drop support for Python 2.7 in January 20 )
- Python使用platform库获取系统信息:操作系统信息、硬件信息、python环境信息
- Windows下python监控脚本详解编程语言
- python-迭代和递归详解编程语言
- Linux 启动 Python编程之旅(linux打开python)
- Python爬取MySQL数据,助力数据分析(python读取mysql数据)
- Python如何使用MySQL构建立连接(python怎么连接mysql)
- python网络编程学习笔记(一)
- python网络编程学习笔记(七):HTML和XHTML解析(HTMLParser、BeautifulSoup)