【问题标题】:Webscraping with Python3 - Ignoring Duplicate Attribute Errors使用 Python 进行 Web 抓取 - 忽略重复的属性错误
【发布时间】:2015-11-01 19:10:46
【问题描述】:

我想使用 Python 3 创建一个网页抓取应用程序。我试图抓取的网站包含无效的 xhtml - 因为它具有带有重复属性名称的标签。

我想使用 xml.dom.minidom 来解析获取的页面。由于重复的属性名称,内容没有解析,并出现以下错误:

Traceback (most recent call last):
  File "scraper.py", line 45, in <module>
    scraper.list()
  File "scraper.py", line 34, in list
    dom = parseString(response.text)
  File "C:\Python34\lib\xml\dom\minidom.py", line 1970, in parseString
    return expatbuilder.parseString(string)
  File "C:\Python34\lib\xml\dom\expatbuilder.py", line 925, in parseString
    return builder.parseString(string)
  File "C:\Python34\lib\xml\dom\expatbuilder.py", line 223, in parseString
    parser.Parse(string, True)
xml.parsers.expat.ExpatError: duplicate attribute: line 2, column 43

我想忽略这个错误并解析文档。我无法控制即将到来的 html 数据。我能做什么?

这是我的代码:

import requests
from xml.dom.minidom import parse, parseString


class Scraper:

    def __init__( self ):

        pass

    def list(self,pages=1):

        response = requests.get('http://example.com')

        dom = parseString(response.text)

        print(dom.toxml)


if __name__ == "__main__":

    scraper = Scraper()

    scraper.list()

【问题讨论】:

    标签: python python-3.x xml-parsing web-scraping html-parsing


    【解决方案1】:

    有一个更好的方法:切换到BeautifulSoup HTML parser。它非常擅长解析格式不正确或损坏的 HTML,并且取决于underlying parser library,可以是less or more lenient

    from bs4 import BeautifulSoup
    import requests
    
    response = requests.get(url).content
    soup = BeautifulSoup(response, "html.parser")  # or use "html5lib", or "lxml"
    

    【讨论】:

      猜你喜欢
      • 2019-03-06
      • 2020-07-30
      • 2020-07-22
      • 2019-01-09
      • 2017-06-12
      • 2020-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多