我正在使用Web API(film API
)。当我使用对某个URL的请求发出发布请求时,得到以下响应:
<a href='\"https:\/\/xdede.co\/peliculas\/p284052-ver-doctor-strange-online\"' up-target='\"body\"'>\n\t\t\t\t\t\t
<div class='\"SPoster\"'>\n\t\t\t\t\t\t\t
<img src='\"https:\/\/image.tmdb.org\/t\/p\/w45\/7OpmunCEZo93nyRIbx59QRaFvZz.jpg\"'/>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t
<h2>Doctor Strange<\/h2>\n\t\t\t\t\t\t<span>Pelicula<\/span>\n\t\t\t\t\t<\/a>\n\t\t\t\t<\/div>\n\t\t\t\t"}</span>
</h2></div></a>
如何过滤此混乱以获取href
和h2
标签?我已经尝试过beautifulsoup
,但没有尝试。有什么建议吗?
答案 0 :(得分:1)
使用BeautifulSoup
和regex
import re
import bs4 as bs4
html = """<a href='\"https:\/\/xdede.co\/peliculas\/p284052-ver-doctor-strange-online\"' up-target='\"body\"'>\n\t\t\t\t\t\t<div class='\"SPoster\"'>\n\t\t\t\t\t\t\t<img src='\"https:\/\/image.tmdb.org\/t\/p\/w45\/7OpmunCEZo93nyRIbx59QRaFvZz.jpg\"'/>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t<h2>Doctor Strange<\/h2>\n\t\t\t\t\t\t<span>Pelicula<\/span>\n\t\t\t\t\t<\/a>\n\t\t\t\t<\/div>\n\t\t\t\t"}</span></h2></div></a>"""
soup = bs4.BeautifulSoup(html, features='html.parser')
href = re.sub(r'[\\"]', '', soup.a['href'])
h2 = re.sub(r'<[^>]*>', '', soup.a.h2.text)
h2 = ' '.join(re.findall(r'(\w+)', h2))
print(href)
print(h2)
输出:
https://xdede.co/peliculas/p284052-ver-doctor-strange-online
Doctor Strange Pelicula