2021-01-08python中request.urlopen()和requests.get()方法的区别

本文介绍了Python中urllib的urlopen和requests.get方法在爬虫中的应用,探讨了两者之间的区别。通过示例代码展示了如何使用它们获取网页内容并进行解析。同时,指出了在使用BeautifulSoup解析时需要注意的问题,强调了解码在处理响应内容时的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

from lxml import etree
import requests
from urllib import request
 
url = "http://www.baidu.com/"
req = request.Request(url)
response1 = request.urlopen(req)
html1 = etree.HTML(response1.read())
print("这个是response1: ", response1)#打印出response1是什么鬼
print("这个是type(response1: " , type(response1))#打印出response1的类型
print("这个是type(html1): " , type(html1)) #打印html1的类型
print("这个是html1: " , html1)#打印出response1的用etree解析为html网页元素
#打印网页的文本信息,由于这个response1只能调用一次所以这里会打印出空
print(response1.read().decode())#打印出网页源码的文本信息
 
print('*'*70)
 
response2 = requests.get(url)
html2 = etree.HTML(response2.text)
print("这个是response2: ",response2)#打印出response2的内容
print("这个是type(response2): " , type(response2))#打印出response2的类型
print("这个是type(response2.text): " , type(response2.text))#打印出response2用text输出是的类型
print(type(response2.content.decode()))#打印出内容解码的类型
print("这个是type(html2): ", type(html2))#打印出response2用etree解析为网页元素信息的html2的类型
print("这个是html2 :", html2)#打印出response2用etree解析为网页元素信息的html2
#打印网页的文本信息,用resquests有三种方法可以打印出网页的信息,这三种方法打印出来的都一样的
response2.encoding = 'utf-8'#给网页指定编码信息,是为了用text输出网页文本信息的
print('文本=',response2.text)#打印出网页的文本信息
print('二进制文本解码=',response2.content.decode())#打印出网页源码的文本信息,这样写是为了避免出现中文乱码
print('二进制文本解码并转化成字符串',str(response2.content,"utf-8"))#打印出网页源码的文本信息,这样写是为了避免出现中文乱码

转载别人的,改了几处代码,print后更明显

爬虫里面,我们不可避免的要用urllib中的urlopen()和requests.get()方法去请求或获取一个网页的内容,这里面的区别在于urlopen打开URL网址,url参数可以是一个字符串url或者是一个Request对象,返回的是http.client.HTTPResponse对象.http.client.HTTPResponse对象大概包括read()、readinto()、getheader()、getheaders()、fileno()、msg、version、status、reason、debuglevel和closed函数,其实一般而言使用read()函数后还需要decode()函数,返回的网页内容实际上是没有被解码或的,在read()得到内容后通过指定decode()函数参数,可以使用对应的解码方式。 

requests.get()方法请求了站点的网址,然后打印出了返回结果的类型,状态码,编码方式,Cookies等内容

 

我在刚学到他们的时候也很懵逼,自己慢慢的琢磨,然后用个笨办法写了个这个来做区别

使用方法和他们区别的代码如下:

 
  1.  

结果

 

2

3

4

可以见到最后三个是一样的,就不截图完整了

 

 

以下为原创:

刚刚新遇到一个问题

from urllib import request
from bs4 import BeautifulSoup  
if __name__ == '__main__':
    target = 'https://hao.360.com/?a1004'
    req = request.Request(target)#获得完整响应
    response=request.urlopen(req)#打开完整响应
    print('response***',type(response))
    #需要将html变成text然后用lxml后bs才能解析
    html1=response.read()
    print('html1***',type(html1))
    html=response.read().decode('utf-8')#可见解码后变成了字符串
 
    
    bb=BeautifulSoup(html,'html.parser')#可见bs需要解码html,不能是字符串
    print('html***',type(html))
    texts=bb.find_all('div',class_='cont')
    print(texts)
    print(type(texts))

注意第二段BeautifulSoup解析的是html,而html已经被response.read().decode('utf-8')了,type是str

此时BS解析失败,结果是空的

ff

 

from urllib import request
from bs4 import BeautifulSoup  
if __name__ == '__main__':
    target = 'https://hao.360.com/?a1004'
    req = request.Request(target)#获得完整响应
    response=request.urlopen(req)#打开完整响应
    print('response***',type(response))
    #需要将html变成text然后用lxml后bs才能解析
    html1=response.read()
    print('html1***',type(html1))
    html=response.read().decode('utf-8')#可见解码后变成了字符串
 
    
    bb=BeautifulSoup(html1,'html.parser')#可见bs需要解码html,不能是字符串
    print('html***',type(html))
    texts=bb.find_all('div',class_='cont')
    print(texts)
    print(type(texts))

在这里我还是要吐槽一下csdn代码段插入段落按钮!鼠标移上去就消失了!

接着说,将 bb=BeautifulSoup(html1,'html.parser'),改成html1,而html1=response.read(),type是byte

此时解析成功

tt

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值