python爬虫学习——urllib库
#获取一个get请求
#import urllib.request
# response = urllib.request.urlopen("http://www.baidu.com")
# print(response.read().decode('utf-8')) #对获取到的网页源码进行utf-8解码
'''
#获取一个post请求
import urllib.request
import urllib.parse
data = bytes(urllib.parse.urlencode({"hello":"world"}),encoding = "utf-8")
response = urllib.request.urlopen("http://httpbin.org/post",data = data)
print(response.read().decode("utf-8"))
'''
'''
#超时处理
import urllib.request
try:
response = urllib.request.urlopen("http://httpbin.org/get",timeout=0.01)
print(response.read().decode("utf-8"))
except urllib.error.URLError as e:
print("time out!")
'''
'''
#可以获取的数据
import urllib.request
response = urllib.request.urlopen("http://www.baidu.com")
# print(response.status)
print(response.getheader("Server"))
'''
'''
#伪装成浏览器
import urllib.request
import urllib.parse
url = "http://httpbin.org/post"
data = bytes(urllib.parse.urlencode({"name":"eric"}),encoding='utf-8')
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.50",
"X-Amzn-Trace-Id": "Root=1-63f48078-2f75544f15e5c54a7b905e25"
}
req = urllib.request.Request(url=url,data=data,headers=headers,method="POST")
response = urllib.request.urlopen(req)
print(response.read().decode("utf-8"))
'''
#伪装浏览器爬取豆瓣
import urllib.request
url = "https://www.douban.com"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.50",
"X-Amzn-Trace-Id": "Root=1-63f48078-2f75544f15e5c54a7b905e25"
}
req = urllib.request.Request(url=url,headers=headers)
response = urllib.request.urlopen(req)
print(response.read().decode("utf-8"))
python爬虫学习——urllib库的更多相关文章
- python爬虫之urllib库(三)
python爬虫之urllib库(三) urllib库 访问网页都是通过HTTP协议进行的,而HTTP协议是一种无状态的协议,即记不住来者何人.举个栗子,天猫上买东西,需要先登录天猫账号进入主页,再去 ...
- python爬虫之urllib库(二)
python爬虫之urllib库(二) urllib库 超时设置 网页长时间无法响应的,系统会判断网页超时,无法打开网页.对于爬虫而言,我们作为网页的访问者,不能一直等着服务器给我们返回错误信息,耗费 ...
- python爬虫之urllib库(一)
python爬虫之urllib库(一) urllib库 urllib库是python提供的一种用于操作URL的模块,python2中是urllib和urllib2两个库文件,python3中整合在了u ...
- python爬虫之urllib库介绍
一.urllib库 urllib是Python自带的一个用于爬虫的库,其主要作用就是可以通过代码模拟浏览器发送请求.其常被用到的子模块在Python3中的为urllib.request和urllib. ...
- python 爬虫之 urllib库
文章更新于:2020-03-02 注:代码来自老师授课用样例. 一.初识 urllib 库 在 python2.x 版本,urllib 与urllib2 是两个库,在 python3.x 版本,二者合 ...
- Python 爬虫之urllib库的使用
urllib库 urllib库是Python中一个最基本的网络请求库.可以模拟浏览器的行为,向指定的服务器发送一个请求,并可以保存服务器返回的数据. urlopen函数: 在Python3的urlli ...
- python爬虫入门urllib库的使用
urllib库的使用,非常简单. import urllib2 response = urllib2.urlopen("http://www.baidu.com") print r ...
- python爬虫之urllib库
请求库 urllib urllib主要分为几个部分 urllib.request 发送请求urllib.error 处理请求过程中出现的异常urllib.parse 处理urlurllib.robot ...
- Python爬虫系列-Urllib库详解
Urllib库详解 Python内置的Http请求库: * urllib.request 请求模块 * urllib.error 异常处理模块 * urllib.parse url解析模块 * url ...
- 爬虫学习--Urllib库基本使用 Day1
一.Urllib库详解 1.什么是Urllib Python内置的HTTP请求库 urllib.request 请求模块(模拟实现传入网址访问) urllib.error ...
随机推荐
- Git Pull Failed:You have not concluded your merge.Exiting because of unfinished merge
前言 在拉取远程代码时,出现 Git Pull Failed:You have not concluded your merge.Exiting because of unfinished merge ...
- Nginx 之fastcgi常用配置项说明
在LNMP环境中,我们都知道nginx如果要解析php脚本语言,就必须通过配置fastcgi模块来提供对php支持,那么在配置fastcgi的时候,关于fastcgi配置项的值应该怎么设置才能让其发挥 ...
- Spring定时任务的秘密
Spring定时任务的秘密 在 Spring 框架中,定时任务主要通过 @Scheduled 注解或 TaskScheduler 接口实现. 1.基本使用 在 Spring Boot 项目中,通过 @ ...
- BUUCTF---rsa_output
题目 点击查看代码 {21058339337354287847534107544613605305015441090508924094198816691219103399526800112802416 ...
- [每日算法 - 华为机试] leetcode680. 验证回文串 II
入口 力扣https://leetcode.cn/problems/valid-palindrome-ii/submissions/ 题目描述 给你一个字符串 s,最多 可以从中删除一个字符. 请你判 ...
- FastAPI Pydantic动态调整Schema
title: FastAPI Pydantic动态调整Schema date: 2025/3/29 updated: 2025/3/29 author: cmdragon excerpt: Pydan ...
- 集合体系介绍、collection的使用--java进阶day09
1.集合体系结构 我们要学习的集合大体分为两种,一种是单列集合,一种是双列集合 2.单列集合 单列集合又分为两个派系,分别为list接口和set接口,这两个接口皆是collection接口的子接口 3 ...
- 【SpringMVC】概述
SpringMVC 概述 Spring 为展现层提供的基于 MVC 设计理念的优秀的 Web 框架,是目前最主流的 MVC 框架之一 Spring3.0 后全面超越 Struts2,成为最优秀的 MV ...
- ilruntime记录
https://www.jianshu.com/p/e7283e1ed86a
- SpringBoot3整合SpringSecurity6(二)SpringSecurity默默的干了些啥
写在前面 第一节中,我们基本上就引入SpringSecurity依赖,其他什么都没做就完成了认证功能. 之所以我们不用做什么,是因为SpringSecurity默认实现了很多功能. 当然了,这里默认实 ...