简体   繁体   English

一种通过 Python 编程以使用当前日期在本网站下载 CSV 的方法?

[英]A way to program through Python to download CSV in this website using the current date?

Would like to know if there is a way to download from this website https://pselookup.vrymel.com/ in the end of day report part?想知道是否有办法从这个网站下载https://pselookup.vrymel.com/在一天结束的报告部分? I have tried downloading it using requests by inputting the date but the file I'm getting is different from the actual file on click.我尝试通过输入日期使用requests下载它,但我得到的文件与点击时的实际文件不同。

url = 'https://pselookup.vrymel.com/'
r = requests.get(url, allow_redirects=True)
dateToday = datetime.today().strftime('%Y-%m-%d')
csvFile = dateToday + ".csv"
open(csvFile, 'wb').write(r.content)

There are multiple things wrong with your code:您的代码有很多问题:

  • currently the only thing that you are downloading is the page under https://pselookup.vrymel.com/ itself.目前您正在下载的唯一内容是https://pselookup.vrymel.com/下的页面本身。 You need to post something to this site in order to get the response you want.您需要在此站点上发布一些内容才能获得所需的响应。 I suggest you read the documentation for the requests package我建议您阅读请求 package 的文档

  • you are using the date of the current day but as far as I can see, the site only puts out reports for finished days, meaning at some point of the day (after pselookup put out the report for the current day), your date is correct.您使用的是当天的日期,但据我所知,该网站仅发布完成日期的报告,这意味着在当天的某个时间点(在 pselookup 发布当天的报告之后),您的日期是正确的。 But before the new report has been put out, you want your code to correct your date for -1 day.但是在新报告发布之前,您希望您的代码将您的日期更正为 -1 天。 An example: today is the 24th and your code is asking for the report from the 24th.一个例子:今天是 24 日,您的代码要求 24 日的报告。 BUT the report from this day has not yet been put out, instead you want to ask for the report from the 23rd.但是今天的报告还没有出,你要索取23号的报告。 Does that make sense?那有意义吗?

  • generally, using the open() method is considered unsafe and it is recommended to use通常,使用 open() 方法被认为是不安全的,建议使用

with open() as <filename of your choosing>:

so you might want to use所以你可能想使用

with open(csvFile, "wb") as file:
    file.write(r.content)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM