【Pyhton爬虫一】requests与BeautifulSoup
来源:互联网 发布:php 获取共享文件 编辑:程序博客网 时间:2024/05/18 08:30
requests与BeautifulSoup基础入门
1. 前言
最近在学习python爬虫,以前实现python爬虫,主要是使用较为底层的urllib和urllib2来实现的,这种方法最原始,编码起来也比较困难。而采用requests + BeautifulSoup的实现方案,可以简化代码的书写。如果有不好和错误的地方希望大佬指出。
2. 介绍
- 在使用这两个模块之前,需要对这两个模块做一些介绍:requests是基于urllib,采用 Apache2 Licensed 开源协议的 HTTP 库,比 urllib 更加方便。BeautifulSoup是一个可以从HTML或XML文件中提取数据的Python库,实际上,它将html中的tag作为树节点进行解析。
- requests官方文档:http://docs.python-requests.org/zh_CN/latest/user/quickstart.html
- BeautifulSoup官方文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html
3. 实现代码
首先是引入这2个库,这里我使用的是PyCharm编辑器,通过Settings→Project: WorkSpace→Project Interpreter寻找bs4和requests库。pip方法引入第三方库请自行百度。
先从最简单的开始,点进糗事百科首页
import requests # 导入requests模块res = requests.get("http://www.qiushibaike.com") # 获取糗事百科首页print (res.text) # print(res)打印的是响应码,print(res.text)打印的是首页的源代码
得到页面源码,如果发现页面文字是乱码,则是编码的原因,输出页面的编码
print (res.encoding)
如果不是UTF-8,可以设置为UTF-8
res.encoding = "utf-8"
点进一篇文章,按F12进入开发者工具,按住ctrl+shift+c或者是点击左上角的剪头选中页面中的文章
发现其class是content
# 获取文章内容import requestsfrom bs4 import BeautifulSoupres = requests.get("https://www.qiushibaike.com/article/119567920")soup = BeautifulSoup(res.text, "html.parser") # 把我们需要的内容放到BeautifulSoup中,html.parser是一个解析器div = soup.find_all(class_="content")[0] # 找寻class为content的内容print(div.text.strip()) # 输出文章内容
如果要获取首页一页的文章内容,则通过开发者工具查看首页,发现每个文章的页面class为article block untagged mb15 typs_xxxx
用re来匹配各种文章的class。
Python3正则表达式:http://www.runoob.com/python3/python3-reg-expressions.html
# 获取所有文章的内容import requestsfrom bs4 import BeautifulSoupimport reres = requests.get("http://www.qiushibaike.com")soup = BeautifulSoup(res.text, "html.parser")divs = soup.find_all(class_=re.compile(r'article block untagged mb15 typs_(\w*)')) # 所有文章是一个数组for div in divs: # 循环取出 joke = div.span.get_text() print(joke.strip()) print("------")
输出内容后发现有些内容读起来很奇怪,看页面发现有些是有图片的,图片的网页标签(HTML tag)是img。
所以我们要把有图片的文章过滤掉,发现有图片文章有个class为thumb,则我们把有图片的过滤掉
# 获取一页没有图片的文章import requestsfrom bs4 import BeautifulSoupimport reres = requests.get("http://www.qiushibaike.com")soup = BeautifulSoup(res.text, "html.parser")divs = soup.find_all(class_=re.compile(r'article block untagged mb15 typs_(\w*)')) # 匹配classfor div in divs: if div.find_all(class_="thumb"): # 如果有图片则过滤 continue joke = div.span.get_text() print(joke.strip()) print("------")
但是糗事百科有很多页,点击第二页发现网址为:https://www.qiushibaike.com/8hr/page/2/ ,点击第三页发现网址为:https://www.qiushibaike.com/8hr/page/3 ,所以我们只需要将网址最后的数字变动即可得到其他页面
# 获取前几页的文章import requestsfrom bs4 import BeautifulSoupimport rebase_url = "https://www.qiushibaike.com/8hr/page/"for num in range(1, 3): # 设置循环,让num分别等于1-3,获取前3页内容 print('第{}页:'.format(num)) res = requests.get(base_url + str(num)) # 这里对网址后面加上数字 soup = BeautifulSoup(res.text, "html.parser") divs = soup.find_all(class_=re.compile(r'article block untagged mb15 typs_(\w*)')) for div in divs: if div.find_all(class_="thumb"): continue joke = div.span.get_text() print(joke.strip()) print("------") print("\n\n\n\n\n\n\n")
- 【Pyhton爬虫一】requests与BeautifulSoup
- Python 爬虫实战(一):使用 requests 和 BeautifulSoup
- python 爬虫试手 requests+BeautifulSoup
- requests+BeautifulSoup爬虫示例程序
- requests和BeautifulSoup组合爬虫技术
- 【Python爬虫】requests+Beautifulsoup存入数据库
- Python爬虫入门之一-requests+BeautifulSoup
- Python爬虫包 BeautifulSoup 学习(一) 简介与安装
- Python网络爬虫与信息提取(一) requests库
- 用BeautifulSoup,urllib,requests写twitter爬虫(1)
- 使用requests+beautifulsoup模块实现python网络爬虫功能
- 爬虫:用requests和BeautifulSoup爬取网上图片
- Python爬虫实例——基于BeautifulSoup和requests实现
- 当当网畅销书排行爬虫(requests+BeautifulSoup)
- python简单爬虫开发(urllib2、requests + BeautifulSoup)
- Python爬虫笔记之用BeautifulSoup及requests库爬取
- python股票数据爬虫requests、etree、BeautifulSoup学习
- Python下基于requests及BeautifulSoup构建网络爬虫
- 解决android运行启动两个虚拟机的问题
- [BZOJ]1486: [HNOI2009]最小圈 01分数规划+spfa
- javascript
- 作业
- Python数据结构之单链表
- 【Pyhton爬虫一】requests与BeautifulSoup
- 算法的基本特征
- Oracle
- javaScript收藏的一些语句
- CodeForces
- JAVA_HOME环境变量失效的解决办法
- MyBatis传入参数为list、数组、map写法
- SQL语句 对表的某一个字段进行排重 查询
- 裴蜀定理详解+例题: BZOJ 1441 MIN