首页 > 技术文章 > python中html解析

hanjiajiejie 2018-06-11 16:49 原文

import requests
from bs4 import BeautifulSoup

url = "..."

payload =...
headers = None

response = requests.request("POST", url, data=payload, headers=headers)

#print(response.text),type(response.text)
result=str(response.text)
soup=BeautifulSoup(result,"html.parser")
#print soup.body.string
a=soup.body.string.strip()
print a

错误处理:

init.py:166: UserWarning: No parser was explicitly specified

错误提示:To get rid of this warning, change this: BeautifulSoup([your markup]) 
to this: 
BeautifulSoup([your markup], “html.parsar”) 
markup_type=markup_type))

按照错误提示,将代码里的 BeautifulSoup([your_markup]) 在[your_markup]后面加上”html.parsar”即可 

 

推荐阅读