python 0基础学习笔记14:爬虫

#爬取网页
import urllib.request
#向指定的url地址发起请求,并返回服务器响应的数据(文件对象)
response = urllib.request.urlopen('http://www.baidu.com')
#读取文件的全部内容,会把读取到的数据赋值给一个字符串变量
data = response.read()#.decode('utf-8')
print(data)
print(type(data))
#读取一行
data1 = response.readline()
#读取文件的全部内容,会把读取到的数据赋值给一个列表变量
data2 = response.readlines()
print(data2)
print(type(data2))
print(len(data2))
print(type(data2[100].decode('utf-8')))#编码以后才是字符串
# #将爬取到的网页写入到文件
with open(r'E:\程序\MLP\爬虫\file\file.html','wb') as f:
    f.write(data)

#response属性:
print(response.info())#但会当前环境的有关信息
print(response.getcode())#返回状态码,200表示成功,304说明有缓存
if response.getcode() == 200 or response.getcode() ==304:
    #处理
    pass
print(response.geturl())#返回当前正在爬取的URL地址
url = r''
print(urllib.response.unquote(url))#解码
print(urllib.response.quote(url))#编码

#法二:将爬取到的网页直接写入文件,urlretrieve在执行过程中,会产生一些缓存
urllib.request.urlretrieve('http://www.baidu.com',filename=r'E:\程序\MLP\爬虫\file\file2.html')
#清除缓存
urllib.request.urlcleanup()

#设置超时,若网页长时间为响应,系统判断超时,无法爬去
for i in range(1,100):
    try:
        responce = urllib.request.urlopen('http://www.baidu.com',timeout= 0.5)
        print(len(responce.read().decode('utf-8')))
    except:
        print('请求超时,继续下一个爬取')

HTTP请求:进行客户端与服务器端之间的消息传递时使用:

GET:通过URL网址传递信息,可以直接在URL网址上添加要传递的信息
POST:可以向服务器提交数据,是一种比较流行的比较安全的数据传递方式
PUT:请求服务器存储一个资源,通常要指定存储的位置
DELETE:请求服务器删除一个资源
HEAD:请求获取对应的HTTP报头信息
OPTIONS:可以获取当前UTL所支持的请求类型

#Get请求:特点:把数据拼接到请求路径的后面传递给服务器,优点是速度快,缺点是承载的数据量小,不安全
import urllib.request
url = '这里写服务器地址'
response = urllib.request.urlopen(url)
data = response.read().decod('utf-8')
print(data)
print(type(data))

json数据解析:

概念:一种保存数据的格式
作用:可以保存本地的json文件,也可以将json串进行传输,通常将json称为轻量级的传输方式
json文件组成:
{}:代表对象(字典)
[]:代表牌列表
:代表键值对
,分隔两个部分

#将json格式的字符串转为python数据类型的对象
import json
jsonStr = '{"name":"tom","age":19}'#xml文件的内容
jsonData = json.loads(jsonStr)
print(jsonData)#{'name': 'tom', 'age': 19}
print(type(jsonData))#<class 'dict'>

#将python数据类型的对象转为json格式的字符串
jsonData2 = {'name': 'tom', 'age': 19}
jsonStr2 = json.dumps(jsonData2)
print(jsonStr2)#{"name": "tom", "age": 19}
print(type(jsonStr2))#<class 'str'>

#读取本地的json文件:
path1 = r''
with open(path1,'rb') as f:
    data = json.load()#不加s就是读取本地
    print(data)
    print(type(data))#<class 'dict'>此时读的是字典类型

#写本地的json文件:
path2 = r''
jsonData3 = {'name': 'tom', 'age': 19}#现在是字典格式
with open(path2,'w') as f:
    json.dump(jsonData3,f)#写进去就是json格式

Post请求:
特点:把参数进行打包,单独传输
优点:数量大,安全(当对服务器数据进行修改时建议使用post)
缺点:速度慢

import urllib.request
import urllib.parse

ur1 = '这里写服务器地址'
#将要发送的数据合成一个字典,字典的键值去网址里找,一般为input标签的name属性的值
data = {'username':'tom','passwd':'666'}
#将要发送的数据进行打包,记住编码
postData = urllib.parse.urlencode(data).encode('utf-8')
#请求体
req = urllib.request.Request(ur1,data=postData)
#请求
req.add_header('........')
response = urllib.request.urlopen(req)
print(response.read().decode('utf-8'))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值