【问题标题】:BeautifulSoup </div> scraping suddenly stopped workingBeautifulSoup </div> 抓取突然停止工作
【发布时间】:2017-07-03 23:34:10
【问题描述】:

我正在尝试从 NASA 网站上抓取一些 divs 并将所有内容放在一个列表中。 此代码之前有效,突然决定不这样做。除了添加一些打印语句之外,我没有故意更改任何内容,所有这些语句都没有返回任何内容或 [] 没有任何类型的错误。

import re
import urllib2 
from BeautifulSoup import BeautifulSoup as soup
import ssl


url = "https://climate.nasa.gov"
context = ssl._create_unverified_context()
gcontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
web_soup = soup(urllib2.urlopen(url,  context=context))

l = []

# get main-content div
main_div = web_soup.findAll(name="div", attrs={'class': 'change_number'})
print main_div
for element in main_div:
    print element
    l.append(float(str(element)[27:-7]))

print l

非常感谢任何有关查明此突然错误的帮助!

UPDATE1:刚刚在解释器中尝试过,同样失败。 main_div 似乎正在返回 []

UPDATE2:刚刚检查了网站以确保 div change_number 存在。确实如此。

UPDATE3:现在我真的很困惑。我有这段代码,我很确定它与上面的基本相同:

import re
import urllib2 
from BeautifulSoup import BeautifulSoup as soup
import ssl


url = "https://climate.nasa.gov"
context = ssl._create_unverified_context()
gcontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
web_soup = soup(urllib2.urlopen(url,  context=context))

l = []

# get main-content div
main_div = web_soup.findAll(name="div", attrs={'class': 'change_number'})
for element in main_div:
    print element
    l.append(float(str(element)[27:-7]))

print l

但它在 websoup 定义行上抛出了UnicodeEncodeError

Traceback (most recent call last):
  File "climate_nasa_change.py", line 10, in <module>
    web_soup = soup(urllib2.urlopen(url,  context=context))
  File "C:\Python27\lib\site-packages\BeautifulSoup.py", line 1522, in __init__
    BeautifulStoneSoup.__init__(self, *args, **kwargs)
  File "C:\Python27\lib\site-packages\BeautifulSoup.py", line 1147, in __init__
    self._feed(isHTML=isHTML)
  File "C:\Python27\lib\site-packages\BeautifulSoup.py", line 1189, in _feed
    SGMLParser.feed(self, markup)
  File "C:\Python27\lib\sgmllib.py", line 104, in feed
    self.goahead(0)
  File "C:\Python27\lib\sgmllib.py", line 143, in goahead
    k = self.parse_endtag(i)
  File "C:\Python27\lib\sgmllib.py", line 320, in parse_endtag
    self.finish_endtag(tag)
  File "C:\Python27\lib\sgmllib.py", line 358, in finish_endtag
    method = getattr(self, 'end_' + tag)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 11-12: ordinal not in range(128)

UPDATE4:aaaaaaaaaaaa,现在它神奇地工作了。我真的不知道现在到底发生了什么。

UPDATE5:现在它坏了。我发誓我不会改变。认真考虑驱除我的电脑。请帮忙。

UPDATE6:刚刚尝试 ping Climate.nasa.gov。尽管页面始终在我的浏览器中加载,但它并不总是通过。这会导致 BeautifulSoup 失败吗?

【问题讨论】:

    标签: python html python-2.7 web-scraping beautifulsoup


    【解决方案1】:

    问题在于该网站有时会返回 gzip 编码的响应,而有时会返回纯文本。
    如果您使用requests,您可以轻松解决此问题,因为它会自动解码内容:

    web_soup = soup(requests.get(url, verify=False).text)  
    

    注意requests 不是标准库,你必须安装它。
    如果您坚持使用urllib2,您可以使用zlib 解码响应(如果它已编码):

    decode = lambda response : zlib.decompress(response, 16 + zlib.MAX_WBITS) 
    response = urllib2.urlopen(url,  context=context)
    headers = response.info()
    html = decode(response.read()) if headers.get('content-encoding') == 'gzip' else response
    web_soup = soup(html)
    

    【讨论】:

    • 我收到错误requests not defined。请求是一个单独的模块还是在 BeautifulSoup 中?
    猜你喜欢
    • 2017-08-22
    • 2021-02-01
    • 2016-12-29
    • 2018-05-09
    • 2015-12-07
    • 2015-08-23
    • 2014-02-05
    • 2013-09-06
    相关资源
    最近更新 更多