requests 库和beautifulsoup库
python 爬虫和解析
库的安装:pip install requests; pip install beautifulsoup4
requests 的几个常用方法:
requests.request() #以下各方法的基础
requests.get(url,params=None,**kwargs) #获取html内容
requests.head() #获取网页头部内容
requests.post()
requests.put()
requests.patch()
requests.delete()
重点为:get()其有12个控制关键字参数 返回为response对象
r.status_code #200为正常
r.text #html内容
r.encoding 编码
r.apparent_encoding 备选编码
r.content 二进制形式返回,爬取 图片,视频,音频等的关键
常使用try,except框架
import requests
import os
url = 'http://image.ngchina.com.cn/2018/1010/20181010031434134.jpg'
root = 'd://pics//'
path = root + url.split('/')[-1]
try:
if not os.path.exists(root):
os.mkdir(root)
if not os.path.exists(path):
r=requests.get(url)
with open(path,'wb') as f:
f.write(r.content)
f.close()
print('文件保存成功')
else:
print('文件已存在')
except:
print('失败')
import requests
import os
url = 'http://mov.bn.netease.com/open-movie/nos/mp4/2016/05/16/SBM8NN8G6_shd.mp4'
root = 'd://vidio//'
path = root + url.split('/')[-1]
try:
if not os.path.exists(root):
os.mkdir(root)
if not os.path.exists(path):
r=requests.get(url)
with open(path,'wb') as f:
f.write(r.content)
f.close()
print('文件保存成功')
else:
print('文件已存在')
except:
print('失败')
1 import requests
2 from bs4 import BeautifulSoup
3 import bs4
4 def gethtmltext(url):
5 try:
6 r = requests.get(url,timeout=30)
7 r.raise_for_status()
8 r.encoding=r.apparent_encoding
9 return r.text
10 except:
11 return ''
12
13
14 def fillunivlist(ulist,html):
15 soup = BeautifulSoup(html,'html.parser')
16 for tr in soup.find('tbody').children:
17 if isinstance(tr,bs4.element.Tag):
18 tds = tr('td')
19 ulist.append([tds[0].string,tds[1].string,tds[2].string])
20
21 def printunivlist(ulist,num):
22 print('{:^10}\t{:^6}\t{:^10}'.format('排名','学校名称','总分'))
23 for i in range(num):
24 u=ulist[i]
25 print('{:^10}\t{:^6}\t{:^10}'.format(u[0],u[1],u[2]))
26
27
28 def main():
29 uinfo = []
30 url = 'http://www.zuihaodaxue.cn/zuihaodaxuepaiming2016.html'
31 html = gethtmltext(url)
32 fillunivlist(uinfo,html)
33 printunivlist(uinfo,20)
34
35 main()
查看爬虫协议在最后加上robots.txt 如:www.jd.com/robots.txt
Beautiful Soup库 #解析网页用
BeautifulSoup(text,'html.parser')
SOUP库的基本元素:
Tag 标签,最基本的信息单元,对应<>....</>
Name 标签名
attributes 标签属性:Tag.attrs
Navigablestring 标签内非属性字符串<>....</>中的字符串 格式:Tag.string
Comment 标签的注释部分
如:<p class='title'>.....</p> p标签
p.name p.attrs p.string
requests 库和beautifulsoup库的更多相关文章
- python爬虫学习(一):BeautifulSoup库基础及一般元素提取方法
最近在看爬虫相关的东西,一方面是兴趣,另一方面也是借学习爬虫练习python的使用,推荐一个很好的入门教程:中国大学MOOC的<python网络爬虫与信息提取>,是由北京理工的副教授嵩天老 ...
- Python:requests库、BeautifulSoup4库的基本使用(实现简单的网络爬虫)
Python:requests库.BeautifulSoup4库的基本使用(实现简单的网络爬虫) 一.requests库的基本使用 requests是python语言编写的简单易用的HTTP库,使用起 ...
- BeautifulSoup库整理
BeautifulSoup库 一.BeautifulSoup库的下载以及使用 1.下载 pip3 install beautifulsoup4 2.使用 improt bs4 二.BeautifulS ...
- 爬虫 Http请求,urllib2获取数据,第三方库requests获取数据,BeautifulSoup处理数据,使用Chrome浏览器开发者工具显示检查网页源代码,json模块的dumps,loads,dump,load方法介绍
爬虫 Http请求,urllib2获取数据,第三方库requests获取数据,BeautifulSoup处理数据,使用Chrome浏览器开发者工具显示检查网页源代码,json模块的dumps,load ...
- $python爬虫系列(2)—— requests和BeautifulSoup库的基本用法
本文主要介绍python爬虫的两大利器:requests和BeautifulSoup库的基本用法. 1. 安装requests和BeautifulSoup库 可以通过3种方式安装: easy_inst ...
- 利用python的requests和BeautifulSoup库爬取小说网站内容
1. 什么是Requests? Requests是用Python语言编写的,基于urllib3来改写的,采用Apache2 Licensed 来源协议的HTTP库. 它比urllib更加方便,可以节约 ...
- Python爬虫小白入门(三)BeautifulSoup库
# 一.前言 *** 上一篇演示了如何使用requests模块向网站发送http请求,获取到网页的HTML数据.这篇来演示如何使用BeautifulSoup模块来从HTML文本中提取我们想要的数据. ...
- 网络爬虫BeautifulSoup库的使用
使用BeautifulSoup库提取HTML页面信息 #!/usr/bin/python3 import requests from bs4 import BeautifulSoup url='htt ...
- 基于BeautifulSoup库的HTML内容的查找
一.BeautifulSoup库提供了一个检索的参数: <>.find_all(name,attrs,recursive,string,**kwargs),它返回一个列表类型,存储查找的结 ...
随机推荐
- 初识ABP vNext(11):聚合根、仓储、领域服务、应用服务、Blob存储
Tips:本篇已加入系列文章阅读目录,可点击查看更多相关文章. 目录 前言 开始 聚合根 仓储 领域服务 BLOB存储 应用服务 单元测试 模块引用 最后 前言 在前两节中介绍了ABP模块开发的基本步 ...
- SpringBoot+RabbitMQ 方式收发消息
本篇会和SpringBoot做整合,采用自动配置的方式进行开发,我们只需要声明RabbitMQ地址就可以了,关于各种创建连接关闭连接的事都由Spring帮我们了~ 交给Spring帮我们管理连接可以让 ...
- 花时三月 终于Spring Boot 微信点餐开源系统! 附源码
架构 前后端分离: Nginx与Tomcat的关系在这篇文章,几分钟可以快速了解: https://www.jianshu.com/p/22dcb7ef9172 补充: set ...
- 错误: 在类中找不到 main 方法, 请将 main 方法定义为:    public static void main(String[] args) 否则 JavaFX 应用程序类必须扩展javafx.application.Application 。
昨天在eclipse编写JAVA程序时,遇到一个问题: 错误: 在类中找不到 main 方法, 请将 main 方法定义为: public static void main(String[] a ...
- 日志分析平台ELK之日志收集器logstash
前文我们聊解了什么是elk,elk中的elasticsearch集群相关组件和集群搭建以及es集群常用接口的说明和使用,回顾请查看考https://www.cnblogs.com/qiuhom-187 ...
- Mac 效率工具必备神器 —— Alfred
前言 alfred 这款软件称为「神器」真是当之无愧.今天专门总结一下,作为之前 Mac 配置教程-开发篇 的补充. 需要说明的是,如果你发现我介绍的功能无法使用,则代表需要花钱购买它的 Powerp ...
- VS调试时查看动态数组的全部元素
转载:https://blog.csdn.net/sinat_36219858/article/details/80720527
- 【CSP2019-J】游记
看我朋友们的博客里面都写了游记,我也来凑个热闹(雾) day1# 介于是\(CSP-J\),我们是比赛当天走的,上午卡点到.一路上不允许玩游戏,于是就在路上看了一路的鬼畜视频,然后看了看对拍的板子(然 ...
- 远程触发Jenkins的Pipeline任务的并发问题处理
前文概述 本文是<远程触发Jenkins的pipeline任务>的续篇,上一篇文章实战了如何通过Http请求远程触发指定的Jenkins任务,并且将参数传递给Jenkins任务去使用,文末 ...
- 对lambda表达式的字节码实现个人理解 - 简单描述
暂且抛开具体的代码实现,谈谈个人的理解. 常规的方法调用,具体由哪条指令来执行,实际都是在JVM的规则中就定下来了,比如构造方法使用invokeSpecial,静态方法使用invokeStatic.现 ...