python - 如何修复此名称未定义错误?
问题描述
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 值
解决方案
您需要创建一个请求会话:
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'
现在,你只需要弄清楚如何不被禁止:)
推荐阅读
- reactjs - 有没有办法使用 JW Player 设置请求标头
- java - 导航抽屉未成功创建
- git - 无法连接到存储库:执行命令时出错:git ls-remote -h git@gitlab.com
- google-apps-script - 如何在文档中的特定位置插入?(在文本项内?)
- c - C 上的 libmongoc-1.0.pc / MongoDB 的问题
- javascript - 画布打字稿错误
- c# - C#访问数据库连接问题
- java - 启用或禁用 MapLoader/MapStore,或在 Hazelcast 中动态提升/降级客户端到服务器或服务器到客户端
- r - 就因子限制而言,另一个但明显的 lme 对比错误
- javascript - How do I loop through an array in Vue half the amount of times as there are items?