python - 在 python 属性中使用 beautiful soup 'contents'

标签 python beautifulsoup

我正在使用 bool“Hello! Python”中的以下代码:

import urllib2
from bs4 import BeautifulSoup
import os

def get_stock_html(ticker_name):
    opener = urllib2.build_opener(urllib2.HTTPRedirectHandler(),urllib2.HTTPHandler(debuglevel=0),)
    opener.addhaders = [('User-agent', "Mozilla/4.0 (compatible; MSIE 7.0; " "Windows NT 5.1; .NET CLR 2.0.50727; " ".NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)")]
    url = "http://finance.yahoo.com/q?s=" + ticker_name
    response = opener.open(url)
    return ''.join(response.readlines())

def find_quote_section(html):
    soup = BeautifulSoup(html)
    # quote = soup.find('div', attrs={'class': 'yfi_rt_quote_summary_rt_top'})
    quote = soup.find('div', attrs={'class': 'yfi_quote_summary'})
    return quote

def parse_stock_html(html, ticker_name):
    quote = find_quote_section(html)
    result = {}
    tick = ticker_name.lower()

    result['stock_name'] = quote.find('h2').contents[0]

if __name__ == '__main__':
    os.system("clear")
    html = get_stock_html('GOOG')
    # print find_quote_section(html)
    print parse_stock_html(html, 'GOOG')

出现以下错误:

Traceback (most recent call last):
  File "dwlod.py", line 33, in <module>
    print parse_stock_html(html, 'GOOG')
  File "dwlod.py", line 25, in parse_stock_html
    result['stock_name'] = quote.find('h2').contents[0]
AttributeError: 'NoneType' object has no attribute 'contents'

我是个新手,不知道该怎么做。难道这本书错了?

已添加

我刚刚将 result['stock_name'] = quote.find('h2').contents[0] 替换为:

x = BeautifulSoup(html).find('h2').contents[0]
return x

现在,没有返回任何内容,但错误不再出现。那么,原来的python语法有问题吗?

最佳答案

虽然雅虎财务已经有一段时间没有真正改变他们的布局了,但自从这本书发布以来,他们似乎已经稍微调整了它,您需要的信息,例如包含股票的 h2 信息符号可以在 yfi_rt_quote_summary 中找到,它是位于 yfi_quote_summary

顶部的容器
def find_quote_section(html):
    soup = BeautifulSoup(html)        
    quote = soup.find('div', attrs={'class': 'yfi_rt_quote_summary'})
    return quote

另请注意,如果我们想打印某些内容,则需要返回结果,或者返回None:

def parse_stock_html(html, ticker_name):
    quote = find_quote_section(html)
    result = {}
    tick = ticker_name.lower()
    result['stock_name'] = quote.find('h2').contents[0]
    return result

>>> print parse_stock_html(html, 'GOOG')
{'stock_name': u'Google Inc. (GOOG)'}
>>> 

顺便说一句,请注意 find 只是查找第一个匹配项。

>>> help(BeautifulSoup(html).find)
find(self, name=None, attrs={}, recursive=True, text=None, **kwargs) method of BeautifulSoup.BeautifulSoup instance
    Return only the first child of this Tag matching the given
    criteria.

它似乎是空的,BeautifulSoup 还有 findall 返回所有匹配项。

>>> BeautifulSoup(html).findAll('h2')[3].contents[0]
u'Google Inc. (GOOG)'

看来第四个值就是我们正在寻找的值......不过,我确信您不会这样做,但请不要每次都解析整个文档,这可能会非常昂贵。

关于python - 在 python 属性中使用 beautiful soup 'contents',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12224323/

相关文章:

python - bash 中的标准输出被一行代码阻塞

python Pandas : Finding cosine similarity of two columns

python - BeautifulSoup 查找找到的标签后的下一个特定标签

Python 正则表达式不提取值 - Python 3.x

python - 如何检查用户是否登录(如何正确使用 user.is_authenticated)?

python - 在 get_text() 中用 <br> 标签分隔

python - 对DBus感到困惑

python - 如何将给定 URL 中的值导入到 python 中?

python - 使用 Python 和 BeautifulSoup 解析谷歌学术搜索结果

python - Beautifulsoup,给url添加属性信息(资源id)