[英]how do i fix this name is not defined error?
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 值
您需要创建一个请求 session:
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()
Output:
<?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'
现在,你只需要弄清楚如何不被禁止:)
似乎您还没有定义session
变量是什么。 如果您使用的是requests
模块,我想您会有类似的东西
import requests
requests.get(url)
或者我想你正在使用 selenium session。 请更正。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.