【问题标题】:scraping data that updates every month抓取每月更新的数据
【发布时间】:2020-10-27 08:51:44
【问题描述】:

我一直在尝试抓取数据,但因为我不知道该怎么做而被卡住了。所以我想在这个网站https://fxtop.com/en/historical-exchange-rates.php?A=1&C1=USD&C2=IDR&MA=1&DD1=01&MM1=08&YYYY1=1995&B=1&P=&I=1&DD2=23&MM2=08&YYYY2=2020&btnOK=Go%21 上每月刮取美元 idr 的价格,但有 25 年的跨度,每个月都会更新。这是我的代码,在这段代码中,我正在抓取 1995 年 8 月到 2020 年 8 月(25 年)的数据,但它不是每个月都更新。所以我希望下个月是september 1995 until september 2020。

import pandas as pd
df = pd.read_html(
    "https://fxtop.com/en/historical-exchange-rates.php?A=1&C1=USD&C2=IDR&MA=1&DD1=01&MM1=09&YYYY1=1995&B=1&P=&I=1&DD2=23&MM2=09&YYYY2=2020&btnOK=Go%21", header=0)[-3]
df['Month'] = pd.to_datetime(df["Month"])
df = df.iloc[::-1].reset_index(drop=True)
df.columns = ['month', 'average', 'min', 'max', 'nb_working_days']
df.to_csv("data_usd.csv", index=False)
df = pd.read_csv("data_usd.csv")

pd.set_option('display.max_rows', df.shape[0]+1)
print(df)

【问题讨论】:

  • “它不是每个月都更新”到底是什么意思?你能给出一个准确的例子来说明你所期望的和你得到的吗?
  • 我想要的是数据每月更新一次,跨度为 25 年,bcs 新月份总是有新数据,就像上个月我从 1995 年 8 月到 2020 年 8 月获取数据(手动抓取) 这个月我希望数据从 1995 年 10 月到 2020 年 10 月自行更新
  • 您的代码中没有任何内容可以执行您提到的所需更新。
  • 是的,我实际上是用 Windows 的任务调度程序更新它,所以当我运行该任务时,它会自动抓取

标签: python pandas beautifulsoup screen-scraping


【解决方案1】:

如果您只需要 10 月份的数据,请执行以下操作:

import pandas as pd
df = pd.read_html(
    "https://fxtop.com/en/historical-exchange-rates.php?A=1&C1=USD&C2=IDR&MA=1&DD1=01&MM1=09&YYYY1=1995&B=1&P=&I=1&DD2=23&MM2=09&YYYY2=2020&btnOK=Go%21", header=0)[-3]
df['Month'] = pd.to_datetime(df["Month"])
df = df.iloc[::-1].reset_index(drop=True)
df.columns = ['month', 'average', 'min', 'max', 'nb_working_days']
df.to_csv("data_usd.csv", index=False)
df = pd.read_csv("data_usd.csv")


indexes_to_drop = []

for index,row in df.iterrows():
    if row.month.split("-")[1] != '10':
        indexes_to_drop.append(index)

df.drop(index=indexes_to_drop, inplace = True)

pd.set_option('display.max_rows', df.shape[0]+1)

print(df)

//编辑:啊,我想我明白了!您可以在 URL 中设置显示间隔。这将为您提供从本月到 1995 年本月的结果

import pandas as pd
import datetime

dt = datetime.datetime.today()


df = pd.read_html(
    f"https://fxtop.com/en/historical-exchange-rates.php?A=1&C1=USD&C2=IDR&MA=1&DD1=01&MM1={dt.month}&YYYY1=1995&B=1&P=&I=1&DD2=23&MM2={dt.month}&YYYY2={dt.year}&btnOK=Go%21", header=0)[-3]
df['Month'] = pd.to_datetime(df["Month"])
df = df.iloc[::-1].reset_index(drop=True)
df.columns = ['month', 'average', 'min', 'max', 'nb_working_days']
df.to_csv("data_usd.csv", index=False)
df = pd.read_csv("data_usd.csv")


pd.set_option('display.max_rows', df.shape[0]+1)

print(df)

【讨论】:

  • 你好!我的问题有错误!我真正想要的是从 1995 年 8 月到 2020 年 8 月,每月抓取 25 年跨度的数据,如果我想像本月这样每月更新数据,我必须在 1995 年 10 月到 2020 年 10 月之间更新数据,有什么帮助。
  • 非常感谢!但是我只能在 1995 年 10 月到 2020 年 1 月使用该代码,再次感谢您!
  • 是的,再次检查有一个小错误。总是第 23 天对你来说很重要吗?因为到 11 月初,您可能会在 11 月 23 日之前收到错误
  • 非常感谢!!!!!!!我希望我能像你一样变得更好!
  • 是的,如果它适合你,请接受答案,然后作为正确答案:)
猜你喜欢
  • 1970-01-01
  • 2022-01-09
  • 1970-01-01
  • 1970-01-01
  • 2018-02-03
  • 2021-10-02
  • 2019-04-26
  • 1970-01-01
  • 2014-10-20
相关资源
最近更新 更多