Python爬虫技术3:获取数据_python爬虫 获取data uri scheme 数据(1)

import urllib.request

#获取一个post请求

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”))


模拟结果:  
 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20200909085649746.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzODE5Mjc0,size_16,color_FFFFFF,t_70#pic_center)  
 在httpbin.org网站上看到的结果:  
 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20200909085734148.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzODE5Mjc0,size_16,color_FFFFFF,t_70#pic_center)  
 3.超时处理  
 防止爬虫的网页相应失败而停滞不前



#超时处理(异常处理)
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(“timeout!”)



response=urllib.request.urlopen(“http://douban.com”)

print(response.status)

print(response.getheaders())


![在这里插入图片描述](https://img-blog.csdnimg.cn/20200909091241407.png#pic_center)  
 418表示被网站发现你在爬虫  
 (百度可以,我们改用百度)



#response不仅可以返回请求的网页信息,还可以查看一些头部等等响应信息
response=urllib.request.urlopen(“http://www.baidu.com”)
print(response.getheaders())
print(response.getheader(“Server”))


![在这里插入图片描述](https://img-blog.csdnimg.cn/20200909091905635.png#pic_center)


与百度网站分析相比:  
 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20200909091728607.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzODE5Mjc0,size_16,color_FFFFFF,t_70#pic_center)  
 是可以获取到相应的响应头部信息的。


4.伪装自己  
 从上面的结果可以看到,模拟的User-agent与真实的浏览器的User-agent是不一样的,因此会被识破,所以我们接下来要伪装自己,达到与浏览器真实的响应一样的结果。


例子1:使用post访问httpbin.org



import urllib.parse
url=“http://httpbin.org/post”
headers={
#伪装自己
“User-Agent”: “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.106 Safari/537.36”
}
data=bytes(urllib.parse.urlencode({“name”:“vivian”}),encoding=“utf-8”)
req=urllib.request.Request(url=url,data=data,headers=headers,method=“POST”) #被我们封装的请求对象
response=urllib.request.urlopen(req)
print(response.read().decode(“utf-8”))


![在这里插入图片描述](https://img-blog.csdnimg.cn/20200909093433198.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzODE5Mjc0,size_16,color_FFFFFF,t_70#pic_center)  
 例子2:使用get访问豆瓣



import urllib.parse
url=“https://douban.com”
headers={
“User-Agent”: “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.106 Safari/537.36”
}
req=urllib.request.Request(url=url,headers=headers)
response=urllib.request.urlopen(req)
print(response.read().decode(“utf-8”))


![在这里插入图片描述](https://img-blog.csdnimg.cn/20200909093903535.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzODE5Mjc0,size_16,color_FFFFFF,t_70#pic_center)


二、获取数据  
 ![在这里插入图片描述](https://img-blog.csdnimg.cn/20200908235113149.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzODE5Mjc0,size_16,color_FFFFFF,t_70#pic_center)



#-*- coding = utf-8 -*-
#@Time : 2020/9/8 23:17
#@Author : Vivian
#@File : spider.py
#@Sofeware : PyCharm

from bs4 import BeautifulSoup #网页解析,获取数据
import re #正则表达式,进行文件匹配
import urllib.request,urllib.error #指定URL,获取网页数据
import xlwt #进行excel操作
import sqlite3 #进行SQLite数据库操作

def main():
baseurl=“https://movie.douban.com/top250?start=”
#1.爬取网页
datalist=getData(baseurl)

savepath=".\\豆瓣电影Top250.xls"
#3.保存数据
#saveData(savepath)

askURL("https://movie.douban.com/top250?start=0")

#爬取网页
def getData(baseurl):
datalist=[]
for i in range(0,10): #调用获取页面信息的函数:十次
url=baseurl+str(i*25)
html=askURL(url) #保存获取到的网页源码

(1)Python所有方向的学习路线(新版)

这是我花了几天的时间去把Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。

最近我才对这些路线做了一下新的更新,知识体系更全面了。

在这里插入图片描述

(2)Python学习视频

包含了Python入门、爬虫、数据分析和web开发的学习视频,总共100多个,虽然没有那么全面,但是对于入门来说是没问题的,学完这些之后,你可以按照我上面的学习路线去网上找其他的知识资源进行进阶。

在这里插入图片描述

(3)100多个练手项目

我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了,只是里面的项目比较多,水平也是参差不齐,大家可以挑自己能做的项目去练练。

在这里插入图片描述

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里无偿获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值