在jibberish中以漂亮的结果进行报废

2024-04-29 12:48:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图用BeautifulSoup4刮一个网站,但是在body标签之后的一切都是胡言乱语,并且破坏了终端。 该网站使用utf-8标记,因此我尝试了解码和不同的html解析器,包括html.parser和lxml

obsObj = BeautifulSoup(html.read().decode('utf-8','ignore'), "html5lib")
print(bsObj.prettify())

结果:

<html>
 <head>
 </head>
    <body>
 }zƲu}y┴(M։ʖO┬┌;R° ─H$D◆P⎼^▒&▒└⎻;\␍␍ (Q│P]]]U]]U£œ␉NG/?5˶ض&±├;ӗ/D&▒└⎻;·GW5Q߶/..(ڧ?ڗV*V┘┌[;≥⎻^N0T4ۓ┐'┴┘S7׏; њ#─K

网站上的相关内容包括:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="da" lang="da" dir="ltr" class="js"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

Tags: 标记http终端解析器lang网站htmlbody
2条回答

这对我来说很好

from bs4 import BeautifulSoup as bs4
import requests
import html5lib


def get_data():

    url = 'http://www.fdm.dk/bildatabasen/mazda/mazda3/15-100-hk/6-man-core-2017'
    r = requests.get(url, headers={"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36"})
    html_bytes = r.text
    soup = bs4(html_bytes, 'html5lib')

    res = soup.find("body")
    print(res.prettify())

    return res

test1 = get_data()

返回

<body>
 <header id="header">
  <div id="logo-section-desktop">
   <div class="rowf">
    <div class="small-12 medium-3 large-3 columns">
     <a href="/" id="desktop-logo">
      FDM
     </a>
    </div>
    <div class="small-12 medium-9 large-9 columns">
     <ul class="top-navigation inline-list">
      <li>
       <a href="https://fdm.dk/alt-om-biler/vild-med-biler/motor">
        Motor
       </a>
      </li>
      ...

你可能从网站上得到了压缩数据。像johnashu一样,使用requests library将自动为您解压。您可以手动执行此操作,但这是一个更难的问题

相关问题 更多 >