我该如何解决这个名称未定义错误?

时间:2020-05-22 10:09:27

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

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值

错误: enter image description here

2 个答案:

答案 0 :(得分:1)

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

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'

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

答案 1 :(得分:-1)

似乎您尚未定义session变量是什么。 如果您使用的是requests模块,那么我想您会遇到类似的情况

import requests
requests.get(url)

或者我想您正在使用硒会话。请更正。