关于html
用requests得到的一个html只有一行,看的难受,用pprint.pprint打印成这样了,怎么让他有缩进的摆的好看点?
代码发上来试试{:7_139:} 把代码发上来,帮你改改 Twilight6 发表于 2020-7-29 20:23
代码发上来试试
from lxml import etree
from pyquery import PyQuery as py
import requests
import pprint
url='https://www.zhihu.com/explore'
headers={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'
}
html=requests.get(url,headers=headers).text
pprint.pprint(html)
zltzlt 发表于 2020-7-29 20:26
把代码发上来,帮你改改
from lxml import etree
from pyquery import PyQuery as py
import requests
import pprint
url='https://www.zhihu.com/explore'
headers={
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'
}
html=requests.get(url,headers=headers).text
pprint.pprint(html)
本帖最后由 zltzlt 于 2020-7-29 20:32 编辑
tiger吴 发表于 2020-7-29 20:29
注:先安装 BS4 再使用下面的代码;BeautifulSoup 的 prettify() 方法会格式化 HTML 代码。
另外你的代码不够整洁,帮你美化了一下
from lxml import etree
from pyquery import PyQuery as py
from bs4 import BeautifulSoup
import requests
url = 'https://www.zhihu.com/explore'
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
'(KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'
}
html = requests.get(url, headers=headers).text
soup = BeautifulSoup(html, 'html.parser')
print(soup.prettify()) zltzlt 发表于 2020-7-29 20:31
注:先安装 BS4 再使用下面的代码;BeautifulSoup 的 prettify() 方法会格式化 HTML 代码。
另外你 ...
nice,拜谢大佬
页:
[1]