我试图从网页中的表格中仅解析货币,但我从该网站得到的结果完全不同。缺少的currencies 可在page source 中找到,因此它们是静态的。我哪里错了? This link与我在another post中使用的不同.为了清楚起见,我想提及这一点。
我试过:
import requests
from bs4 import BeautifulSoup
URL = "https://www.forexfactory.com/calendar.php?day=today"
res = requests.get(URL,headers={'User-Agent':'Mozilla/5.0'})
soup = BeautifulSoup(res.text,"lxml")
for item in soup.select("tr.calendar_row"):
currency = item.select_one("td.calendar__currency").get_text(strip=True)
print(currency)
我得到的输出(与该站点中可用的输出非常不同):
JPY
JPY
EUR
EUR
GBP
GBP
GBP
EUR
EUR
GBP
USD
USD
USD
GBP
JPY
AUD
AUD
CNY
CNY
CNY
CNY
我如何使用请求从该站点获取所有货币?
请您参考如下方法:
cookie 确定某种形式的验证,从而确定您看到的结果。您只需要其他答案中的两个。如果您省略第二个,例如下面显示的那些,您的窗口将转移到早上 5:30 开始(仍然返回相同数量的结果)这是默认返回 - 选择除 1 之外的任何其他值,用于“ffverifytimes” , 你会得到同样的窗口。我认为这是对主页区域设置的时间感知的调整?
如果您省略“ffdstonoff”,您的窗口将切换到凌晨 2:30 开始。
添加 cookie "fftimezoneoffset":"1",您可以将窗口从前一天的晚上 11:45 开始。
import requests
from bs4 import BeautifulSoup as bs
cookies={
"ffdstonoff":"1",
"ffverifytimes":"1"
}
r = requests.get('https://www.forexfactory.com/calendar.php?day=today', cookies = cookies)
soup = bs(r.content, 'lxml')
currencies = [item.text.strip() for item in soup.select('.currency')]
print(currencies)
