【问题标题】:Learning web scraping. I get an error of 'NoneType' object has no attribute 'text' when trying to retrieve text in an element学习网页抓取。尝试检索元素中的文本时,出现“NoneType”对象没有属性“文本”的错误
【发布时间】:2020-02-20 04:53:36
【问题描述】:

代码:https://pastebin.com/JeifRAyT

def FetchPrice():
    URLForStock = requests.get('https://finance.yahoo.com/quote/MSFT?p=MSFT')
    soup = bs4.BeautifulSoup(URLForStock.text, "html5lib")
    Price=soup.find('span', {'class': 'Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)'}).text
    return Price

我正在尝试删除的页面:https://finance.yahoo.com/quote/MSFT

我要检索的元素:https://imgur.com/a/S2mo5vN

HTML 如下所示:https://imgur.com/a/vdriBD9

我不明白第 4 行有什么问题。我让它在寻找一个跨度。我也有我正在寻找的课程。我错过了什么?

【问题讨论】:

    标签: python-3.x web-scraping stock


    【解决方案1】:

    【讨论】:

    • #Price = soup.find('div', {'class': 'My(6px) Pos(r) smartman_Mt(6px)'}).find('span').text This我找到的行找到了它。我不明白为什么它必须引用它的父 div 才能获得我需要的文本。
    猜你喜欢
    • 2020-12-09
    • 1970-01-01
    • 2019-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-29
    相关资源
    最近更新 更多