首页 > 解决方案 > 如何修复此名称未定义错误?

问题描述

from bs4 import BeautifulSoup


def us_30():
    page = session.get('https://www.investing.com/indices/us-30-technical')
    soup = BeautifulSoup(page.content, 'html.parser')
    summary = soup.find(id="techStudiesInnerWrap")
    print(summary.div.text)
    name = soup.find("td", class_="first left symbol", string="RSI(14)")
    value = name.find_next('td')
    action = value.find_next('td')
    print(f"Name: {name.text}. Value:{value.text}. Action: {action.span.text}")


us_30()

我试图从网站上获取 rsi 值

错误: 在此处输入图像描述

标签: pythonpython-3.xweb-scraping

解决方案


您需要创建一个请求会话:

import requests
from bs4 import BeautifulSoup


def us_30():
    session = requests.Session()
    page = session.get('https://www.investing.com/indices/us-30-technical')
    soup = BeautifulSoup(page.content, 'html.parser')
    print(soup)
    summary = soup.find(id="techStudiesInnerWrap")
    print(summary.div.text)
    name = soup.find("td", class_="first left symbol", string="RSI(14)")
    value = name.find_next('td')
    action = value.find_next('td')
    print(f"Name: {name.text}. Value:{value.text}. Action: {action.span.text}")


us_30()

输出:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
<head>
<title>403 You are banned from this site.  Please contact via a different client configuration if you believe that this is a mistake.</title>
</head>
<body>
<h1>Error 403 You are banned from this site.  Please contact via a different client configuration if you believe that this is a mistake.</h1>
<p>You are banned from this site.  Please contact via a different client configuration if you believe that this is a mistake.</p>
<h3>Guru Meditation:</h3>
<p>XID: 1557864559</p>
<hr/>
<p>Varnish cache server</p>
</body>
</html>

Traceback (most recent call last):
  File "x.py", line 18, in <module>
    us_30()
  File "x.py", line 11, in us_30
    print(summary.div.text)
AttributeError: 'NoneType' object has no attribute 'div'

现在,你只需要弄清楚如何不被禁止:)


推荐阅读