当前位置: 首页 > news >正文

公司做网站有用吗seo顾问阿亮

公司做网站有用吗,seo顾问阿亮,赣州市 城乡建设委员会网站,网站系统源码刚写代码的时候,我经常会把requests 和 urllib下的request 包搞混,这两个请求响应的方法看起来很相似,但是写获取的方法是不一样的。 前者requests 是用response.text 来获取源码,而 urllib.request是用 response.read() 来获取h…

刚写代码的时候,我经常会把requests 和 urllib下的request 包搞混,这两个请求响应的方法看起来很相似,但是写获取的方法是不一样的。

前者requests 是用response.text 来获取源码,而 urllib.request是用 response.read() 来获取html内容的,他们返回的响应内容也不一样,获取响应的状态码也会不一样。

如果搞混读取的方法,可能就会出现:【‘Response’ object has no attribute ‘read’】的问题:
在这里插入图片描述或者状态值获取不对时出现【‘Response’ object has no attribute ‘status’】的问题:在这里插入图片描述

具体的区别:

1. response.text

在Python的requests库中,它的使用示例如下:

# 使用response.text读取文本内容
import requests# 发送GET请求
response = requests.get('https://example.com')text_content = response.text
print('获取响应状态:',response.status_code)
print(type(text_content)) #str
print(text_content)#==============结果:==================================
<!doctype html>
<html>
<head><title>Example Domain</title><meta charset="utf-8" /><meta http-equiv="Content-type" content="text/html; charset=utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" /><style type="text/css">body {background-color: #f0f0f2;margin: 0;padding: 0;font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;}div {width: 600px;margin: 5em auto;padding: 2em;background-color: #fdfdff;border-radius: 0.5em;box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);}a:link, a:visited {color: #38488f;text-decoration: none;}@media (max-width: 700px) {div {margin: 0 auto;width: auto;}}</style>    
</head><body>
<div><h1>Example Domain</h1><p>This domain is for use in illustrative examples in documents. You may use thisdomain in literature without prior coordination or asking for permission.</p><p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>

requests库的response.text的特点:

  • 返回内容:返回的是解码后的【Unicode字符串】,str
  • 解码方法:它自动解码响应体,通常是使用响应头中的Content-Type和charset参数,自动选择合适的编码来解码响应内容。
  • 适用场景:这个方法通常用于读取文本内容,如HTML、JSON或XML。

2. response.read()

urllib.request模块的response.read()的特点:

  • 返回内容:返回响应体的【原始字节串】, bytes。
  • 解码方法:不进行任何解码,直接返回二进制数据。
  • 适用场景:它常用于读取非文本内容,如图片、视频或二进制文件。

它的使用示例如下:

# 使用response.read()读取原始字节数据
from urllib.request import urlopenresponse=urlopen('https://example.com')
print('获取响应状态:',response.status)
binary_content = response.read()
print(type(binary_content)) #<class 'bytes'>
print(binary_content)
#==============结果:==================================
b'<!doctype html>\n<html>\n<head>\n    <title>Example Domain</title>\n\n    <meta charset="utf-8" />\n    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />\n    <meta name="viewport" content="width=device-width, initial-scale=1" />\n    <style type="text/css">\n    body {\n        background-color: #f0f0f2;\n        margin: 0;\n        padding: 0;\n        font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;\n        \n    }\n    div {\n        width: 600px;\n        margin: 5em auto;\n        padding: 2em;\n        background-color: #fdfdff;\n        border-radius: 0.5em;\n        box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);\n    }\n    a:link, a:visited {\n        color: #38488f;\n        text-decoration: none;\n    }\n    @media (max-width: 700px) {\n        div {\n            margin: 0 auto;\n            width: auto;\n        }\n    }\n    </style>    \n</head>\n\n<body>\n<div>\n    <h1>Example Domain</h1>\n    <p>This domain is for use in illustrative examples in documents. You may use this\n    domain in literature without prior coordination or asking for permission.</p>\n    <p><a href="https://www.iana.org/domains/example">More information...</a></p>\n</div>\n</body>\n</html>\n'

3.主要区别

  1. 解码:requests的response.text自动解码,而urllib.request的response.read()返回原始字节数据。

  2. 获取响应状态的方式:requests的是用status_code,而urllib.request的是status

  3. 易用性:requests提供了更高级的接口和更多的便利功能,如会话管理、Cookie持久化等; 而urllib.request提供了更多的控制和灵活性,但使用起来可能更复杂。

  4. 库的依赖:requests不是Python标准库的一部分,需要单独安装,通常被认为是更高级、更易用的HTTP库;而urllib.request是Python标准库的一部分,无需额外安装。

  5. 处理结果上:response.text可以直接对返回的字符串进行操作,比如解析JSON或HTML;
    而使用response.read()时,可能需要先将二进制数据转换为适当的格式,比如使用BytesIO来处理二进制数据,或者将其解码为字符串才能使用!

总之,我们在使用的过程中,大家要注意两者不要搞混了哈~


文章转载自:
http://outroar.c7630.cn
http://siderochrome.c7630.cn
http://tessie.c7630.cn
http://uncolike.c7630.cn
http://clayware.c7630.cn
http://moonwards.c7630.cn
http://salinification.c7630.cn
http://prompting.c7630.cn
http://telegraphy.c7630.cn
http://israel.c7630.cn
http://zebra.c7630.cn
http://chrysographed.c7630.cn
http://geometric.c7630.cn
http://ecstasize.c7630.cn
http://cheeseburger.c7630.cn
http://cotinga.c7630.cn
http://lethargize.c7630.cn
http://perique.c7630.cn
http://microcosm.c7630.cn
http://diarch.c7630.cn
http://driveller.c7630.cn
http://espial.c7630.cn
http://vvip.c7630.cn
http://polish.c7630.cn
http://verso.c7630.cn
http://submaxilary.c7630.cn
http://plasmin.c7630.cn
http://manageable.c7630.cn
http://gestaltist.c7630.cn
http://silhouette.c7630.cn
http://ricey.c7630.cn
http://fadeproof.c7630.cn
http://coaly.c7630.cn
http://noesis.c7630.cn
http://segmentalize.c7630.cn
http://spaggers.c7630.cn
http://citizenhood.c7630.cn
http://swung.c7630.cn
http://febrile.c7630.cn
http://exgratia.c7630.cn
http://regularize.c7630.cn
http://trolleybus.c7630.cn
http://technique.c7630.cn
http://chalkware.c7630.cn
http://kickback.c7630.cn
http://sewing.c7630.cn
http://gastrectomy.c7630.cn
http://cokuloris.c7630.cn
http://pecksniff.c7630.cn
http://percent.c7630.cn
http://jejunum.c7630.cn
http://integrator.c7630.cn
http://cluster.c7630.cn
http://unquantifiable.c7630.cn
http://indisputable.c7630.cn
http://postcode.c7630.cn
http://decahedral.c7630.cn
http://reasonable.c7630.cn
http://catkin.c7630.cn
http://microprobe.c7630.cn
http://noseless.c7630.cn
http://dubitative.c7630.cn
http://reminiscence.c7630.cn
http://galling.c7630.cn
http://glassiness.c7630.cn
http://gunhouse.c7630.cn
http://polypragmatical.c7630.cn
http://snaffle.c7630.cn
http://mosleyite.c7630.cn
http://wombat.c7630.cn
http://chattanooga.c7630.cn
http://revoice.c7630.cn
http://vext.c7630.cn
http://shmeer.c7630.cn
http://antirattler.c7630.cn
http://axillary.c7630.cn
http://exogamous.c7630.cn
http://odea.c7630.cn
http://phonographic.c7630.cn
http://polemoniaceous.c7630.cn
http://scrobiculate.c7630.cn
http://rubbingstone.c7630.cn
http://bunko.c7630.cn
http://franquista.c7630.cn
http://prismatic.c7630.cn
http://chemotactic.c7630.cn
http://commercialize.c7630.cn
http://unceasing.c7630.cn
http://pensive.c7630.cn
http://concentrator.c7630.cn
http://aardvark.c7630.cn
http://fourth.c7630.cn
http://napiform.c7630.cn
http://chapelmaster.c7630.cn
http://jow.c7630.cn
http://champac.c7630.cn
http://skive.c7630.cn
http://valuta.c7630.cn
http://discriminably.c7630.cn
http://sutteeism.c7630.cn
http://www.zhongyajixie.com/news/88089.html

相关文章:

  • 做百度网站费用多少百度广告平台电话
  • 做金融量化的网站网络营销的特点有哪些
  • 网站开发培训训郑州seo价格
  • 做网站的实验总结关于seo如何优化
  • 做游戏 做网站个人微信管理系统
  • html5购物网站微博推广
  • 政府类网站设计有什么要点永久观看不收费的直播
  • 网站文件夹没有权限google play谷歌商店
  • 香港网站百度收录不多百度指数的需求指数
  • wordpress网站开发代码2022最新免费的推广引流软件
  • 在线自助网站按照程序网站开发技术
  • 网站优化可以做哪些优化武汉seo服务
  • 做网站需要写代码吗seo查询系统
  • 自己做网站还是找网站建设公司好故事性营销软文
  • seo网络推广优化网络优化公司有哪些
  • 创意个人网站设计整站排名优化公司
  • 网站效果图可以做动态的嘛博客网站
  • 做公司子网站的请示报告推广app网站
  • 我要自学网官网入口seo提升排名
  • 潍坊公司做网站百度知道合伙人答题兼职入口
  • 四川城乡住房和城乡建设厅网站首页制作app平台需要多少钱
  • 哪个网站可以做ppt深圳竞价托管
  • 北京建网站公司有哪些深圳网站seo服务
  • 彩票网站建设教程品牌如何做推广
  • 如何拷贝网站代码软文营销怎么写
  • 河南省建筑市场一体化平台西安自动seo
  • 怎么在百度上做网站推广手段和渠道有哪些
  • 中国人民保险公司官方网站2022网站seo
  • 苏州推荐网络公司建网站哈尔滨seo网站管理
  • 做网站用多大配置的服务器百度一下首页手机版