簡體   English   中英

beautifulsoup 4:分段故障(核心轉儲)

[英]beautifulsoup 4: Segmentation fault (core dumped)

我抓了以下頁面:

http://www.nasa.gov/topics/earth/features/plains-tornadoes-20120417.html

但是在調用時遇到了Segmentation fault(core dumped):BeautifulSoup(page_html),其中page_html是來自請求庫的內容。 這是BeautifulSoup的錯誤嗎? 有沒有辦法解決這個問題? 甚至像try一樣的方法...除了幫助我運行我的代碼。 提前致謝。

代碼如下:

import requests
from bs4 import BeautifulSoup

toy_url = 'http://www.nasa.gov/topics/earth/features/plains-tornadoes-20120417.html'
res = requests.get(toy_url,headers={"USER-Agent":"Firefox/12.0"})
page = res.content
soup = BeautifulSoup(page)

此問題是由lxml中的錯誤引起的, 該錯誤在lxml 2.3.5中得到修復。 您可以升級lxml,或使用帶有html5lib或HTMLParser解析器的Beautiful Soup。

肯定是一個bug。 不應該這樣的段錯誤。 我可以重現(4.0.1):

>>> import bs4, urllib2
>>> url = "http://www.nasa.gov/topics/earth/features/plains-tornadoes-20120417.html"
>>> page = urllib2.urlopen(url).read()
>>> soup = bs4.BeautifulSoup(page)
Segmentation fault

在一些二等分之后,它看起來是由DOCTYPE引起的:

>>> page[:page.find(">")+1]
'<!DOCTYPE "xmlns:xsl=\'http://www.w3.org/1999/XSL/Transform\'">'

粗暴的黑客允許bs4解析它:

>>> soup = bs4.BeautifulSoup(page[page.find(">")+1:])
>>> soup.find_all("a")[:3]
[<a href="/home/How_to_enable_Javascript.html" target="_blank">› Learn How</a>, <a href="#maincontent">Follow this link to skip to the main content</a>, <a class="nasa_logo" href="/home/index.html"><span class="hide">NASA - National Aeronautics and Space Administration</span></a>]

知道更多的人可能能夠看到真正發生的事情,但無論如何,這可能會幫助你開始。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM