Skip to main content
 首页 » 编程设计

python之无法以所需方式从网页中获取两个字段

2025年05月04日18emanlee

我在 python 中创建了一个脚本,用于从位于网页中的表中获取两个字段(第 2 列和第 3 列)time 和 currency。脚本正在获取结果,但不是我希望的方式。

Website address

到目前为止我已经写了:

import requests 
from bs4 import BeautifulSoup 
 
URL = "https://www.forexfactory.com/calendar.php?week=this" 
 
res = requests.get(URL) 
soup = BeautifulSoup(res.text,"lxml") 
 
for item in soup.select("tr.calendar_row"): 
    ftime = item.select_one("td.calendar__time").get_text(strip=True) 
    currency = item.select_one("td.calendar__currency").get_text(strip=True) 
    print(ftime,currency) 

我得到的结果:

All Day JPY 
5:00am CNY 
 CNY 
2:00pm USD 
1:59am JPY 
2:00am EUR 
 EUR 
4:30am GBP 
 GBP 
 GBP 

预期结果:

All Day JPY 
3:00pm CNY 
3:00pm CNY 
2:00pm USD 
1:59am JPY 
12:00pm EUR 
12:00pm EUR 
2:30pm GBP 
2:30pm GBP 
2:30pm GBP 

The times I'm getting are different from that site. Moreover, I wish to fill in the blank times with its earlier values.

如何修改现有脚本以获取上面显示的结果?

请您参考如下方法:

问题在于时间列中的空单元格

import requests 
from bs4 import BeautifulSoup 
 
URL = "https://www.forexfactory.com/calendar.php?week=this" 
 
# Make cookie dictionary for setting timezones 
cookies={ 
    "fftimezoneoffset":"0", #timezone / UTC +/-X 
    "fftimeformat":"1", # format 0=am/pm / 1=24hour format 
    "ffdstonoff":"1", # daylight saving 
    "ffverifytimes":"1" # set times to timezone 
} 
res = requests.get(URL,cookies=cookies) # apply timezones 
soup = BeautifulSoup(res.text,"lxml") 
lastTime = "" #lastTime for cases with empty values for times 
for item in soup.select("tr.calendar_row"): 
 
    ftime = item.select_one("td.calendar__time").get_text(strip=True) 
    if len(ftime) == 0: #if empty time use last one  
        ftime = lastTime 
    lastTime = ftime 
    currency = item.select_one("td.calendar__currency").get_text(strip=True) 
    if len(currency) > 0: # print if there is currenty  
        print(ftime,currency)