爬虫——urllib.request包
一、引用包
import urllib.request
二、常用方法
(1)urllib.request.urlretrieve(网址,本地文件存储地址):直接下载网页到本地
urllib.request.urlretrieve("http://www.baidu.com","D:\1.html")
(2)urllib.request.urlcleanup():清理缓存
(3)查看网页基本内容
file = urllib.request.urlopen("http://www.baidu.com")
print(file.info()) #查看网页信息
print(file.code())#查看网页状态码
print(file.geturl())#获取当前网页的url
(4)设置网页超时时间
urllib.request.urlopen("http"//www.baidu.com",timeout=1)
timeout就是网页的超时时间设定
三、POST请求
import urllib.request
import urllib.parse
post_url = "http://www.baidu.com"
post_data = urllib.parse.urlencode{
"username":"username"
"password":"password"
}.encode("utf-8") req = urllib.request.Request(post_url,post_data)
四、异常处理
import urllib.request
import urllib.error try:
urllib.request.urlopen("http://www.baidu.com")
except urllib.error.URLError as e:
if hasattr(e,"code"):
print(e.code)
if hasattr(e,"reason"):
print(e.reason)
爬虫——urllib.request包的更多相关文章
- 爬虫——urllib.request库的基本使用
所谓网页抓取,就是把URL地址中指定的网络资源从网络流中读取出来,保存到本地.在Python中有很多库可以用来抓取网页,我们先学习urllib.request.(在python2.x中为urllib2 ...
- 在python3中使用urllib.request编写简单的网络爬虫
转自:http://www.cnblogs.com/ArsenalfanInECNU/p/4780883.html Python官方提供了用于编写网络爬虫的包 urllib.request, 我们主要 ...
- 网络爬虫urllib:request之urlopen
网络爬虫urllib:request之urlopen 网络爬虫简介 定义:按照一定规则,自动抓取万维网信息的程序或脚本. 两大特征: 能按程序员要求下载数据或者内容 能自动在网络上流窜(从一个网页跳转 ...
- python3爬虫初探(一)之urllib.request
---恢复内容开始--- #小白一个,在此写下自己的python爬虫初步的知识.如有错误,希望谅解并指出. #欢迎和大家交流python爬虫相关的问题 #2016/6/18 #----第一把武器--- ...
- python3.6 urllib.request库实现简单的网络爬虫、下载图片
#更新日志:#0418 爬取页面商品URL#0421 更新 添加爬取下载页面图片功能#0423 更新 添加发送邮件功能# 优化 爬虫异常处理.错误页面及空页面处理# 优化 爬虫关键字黑名单.白名单,提 ...
- 爬虫之urllib.request基础使用(一)
urllib模块 urllib模块简介: urllib提供了一系列用于操作URL的功能.包含urllib.request,urllib.error,urllib.parse,urllib.robotp ...
- Python做简单爬虫(urllib.request怎么抓取https以及伪装浏览器访问的方法)
一:抓取简单的页面: 用Python来做爬虫抓取网站这个功能很强大,今天试着抓取了一下百度的首页,很成功,来看一下步骤吧 首先需要准备工具: 1.python:自己比较喜欢用新的东西,所以用的是Pyt ...
- 爬虫初探(1)之urllib.request
-----------我是小白------------ urllib.request是python3自带的库(python3.x版本特有),我们用它来请求网页,并获取网页源码. # 导入使用库 imp ...
- 爬虫小探-Python3 urllib.request获取页面数据
使用Python3 urllib.request中的Requests()和urlopen()方法获取页面源码,并用re正则进行正则匹配查找需要的数据. #forex.py#coding:utf-8 ' ...
随机推荐
- 全网最全!彻底弄透Java处理GMT/UTC日期时间
目录 前言 本文提纲 版本约定 正文 Date类型实现 时区/偏移量TimeZone 设置默认时区 让人恼火的夏令时 Date时区无关性 读取字符串为Date类型 SimpleDateFormat格式 ...
- gRPC-go源码(1):连接管理
1 写在前面 在这个系列的文章中,我们将会从源码的层面学习和理解gRPC. 整个系列的文章的计划大概是这样的:我们会先从客户端开始,沿着调用路径逐步分析到服务端,以模块为粒度进行学习,考虑这个模块是为 ...
- CentOS 7.2系统安装步骤
CentOS 7.2系统安装步骤 1.把系统U盘插到服务器上,然后启动服务器进入BIOS界面选择U盘启动. 根据服务器的不同,进入BIOS界面的按钮也不一样,主流的有F10.F11.F12.F2.ES ...
- Most basic operations in Go are not synchronized. In other words, they are not concurrency-safe.
Most basic operations in Go are not synchronized. In other words, they are not concurrency-safe. htt ...
- 洛谷P4127
Description 给出两个数 \(a\),\(b\) ,求出 \([a,b]\) 中各位数字之和能整除原数的数的个数 Solution 设 \(f[i][j][k][q]\) 表示 枚举到第 i ...
- COGS 307 模拟退火
307. [HAOI2006] 均分数据 ★★ 输入文件:data.in 输出文件:data.out 简单对比时间限制:3 s 内存限制:128 MB [问题描述] 已知N个正整数:A ...
- MIT 6.S081 Lab File System
前言 打开自己的blog一看,居然三个月没更新了...回想一下前几个月,开题 + 实验室杂活貌似也没占非常多的时间,还是自己太懈怠了吧,掉线城和文明6真的是时间刹手( 不过好消息是把15445的所有l ...
- Ajax(form表单文件上传、请求头之contentType、Ajax传递json数据、Ajax文件上传)
form表单文件上传 上菜 file_put.html <form action="" method="post" enctype="multi ...
- typedef void (*sighandler_t)(int);
typedef void (*sighandler_t)(int); sighandler_t signal(int signum, sighandler_t handler); ---------- ...
- HPE 交换机基础配置
1.交换机命名 (config)# hostname POE-SW 2.vlan创建及端口划分 1)端口加入vlan,两种方式 (config)# vlan 2 (vlan-2)# untagged ...