小甲鱼的铁粉 发表于 2020-7-5 11:46:32

让打印出来的网页信息整齐

刚学requests模块的内容,怎么让r.text打印出来整齐呢,我这个打印出来么有换行欸{:10_269:}
代码
import requests
r = requests.get('https://www.baidu.com/')
print(type(r))
print(r.status_code)
print(type(r.text))
print(r.text)
print(r.cookies)

输出html的代码好乱
<class 'requests.models.Response'>
200
<class 'str'>
<!DOCTYPE html>

<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>&ccedil;™&frac34;&aring;&ordm;&brvbar;&auml;&cedil;€&auml;&cedil;‹&iuml;&frac14;Œ&auml;&frac12;&nbsp;&aring;°±&ccedil;Ÿ&yen;道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus=autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=&ccedil;™&frac34;&aring;&ordm;&brvbar;&auml;&cedil;€&auml;&cedil;‹ class="bg s_btn" autofocus></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>&aelig;–°é—&raquo;</a> <a href=https://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>&aring;œ°&aring;›&frac34;</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视é&cent;‘</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>è&acute;&acute;&aring;§</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>&ccedil;™&raquo;&aring;&frac12;•</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">&ccedil;™&raquo;&aring;&frac12;•</a>');

                </script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">&aelig;›&acute;&aring;¤š&auml;&ordm;§&aring;“</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>&aring;…&sup3;&auml;&ordm;Ž&ccedil;™&frac34;&aring;&ordm;&brvbar;</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>&auml;&frac12;&iquest;&ccedil;”¨&ccedil;™&frac34;&aring;&ordm;&brvbar;&aring;‰&aring;&iquest;…è&macr;&raquo;</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>&aelig;„è§&aring;é&brvbar;ˆ</a>&nbsp;&auml;&ordm;&not;ICPè&macr;030173&aring;·&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>


<RequestsCookieJar[<Cookie BDORZ=27315 for .baidu.com/>]>

青出于蓝 发表于 2020-7-5 11:48:22

本帖最后由 青出于蓝 于 2020-7-5 11:53 编辑

import requests
r =requests.get('https://www.baidu.com/')
r=r.content
r=str(r,"utf-8")
print(r)


原代码出来后用bs4

小甲鱼的铁粉 发表于 2020-7-5 11:50:50

青出于蓝 发表于 2020-7-5 11:48


这个会报错的
Traceback (most recent call last):
File "C:\Users\86188\Desktop\1.py", line 3, in <module>
    r = str(r,"utf-8")
TypeError: decoding to str: need a bytes-like object, Response found

qiuyouzhi 发表于 2020-7-5 11:51:23

import requests
from bs4 import BeautifulSoup as BS
r = requests.get('https://www.baidu.com/')
soup = BS(r.text, "html.parser")
print(soup.prettify())
页: [1]
查看完整版本: 让打印出来的网页信息整齐