【问题标题】:How to use wget or curl to download a php-generated csv file from a webpage?如何使用 wget 或 curl 从网页下载 php 生成的 csv 文件?
【发布时间】:2021-03-14 09:08:24
【问题描述】:

我想下载一个 csv 文件,您可以从以下公共页面下载:

https://www.historique-meteo.net/france/rh-ne-alpes/annecy-haute-savoie/2016/02/

在右上角,您会看到一个下载按钮“导出 CSV”,它将下载一些 csv 文件。 用开发者控制台查看代码你会发现这个按钮的html代码是

<a href="/site/export.php?ville_id=1025&amp;annee=2016&amp;mois=02" class="btn btn-small btn-theme pull-right">export CSV</a>

所以我尝试通过以下命令使用 curl/wget 下载 csv 文件

curl "https://www.historique-meteo.net/site/export.php?ville_id=1025&annee=2016&mois=02"
wget "https://www.historique-meteo.net/site/export.php?ville_id=1025&annee=2016&mois=02"

但我没有得到 csv 文件。我要么得到一些 html 数据,要么得到 503 错误。

如何正确做?

【问题讨论】:

  • 你得到 503 的原因是该页面在 Cloudflare DDOS 保护之后。
  • 但是使用网络浏览器,我可以从该页面下载 csv 文件,该页面位于 Cloudflare DDOS 保护之后。我不能模拟浏览器的功能吗?

标签: html csv curl wget


【解决方案1】:

在浏览器中转到该页面,单击Export CSV,越过 Cloudflare DDOS 保护屏幕,取消下载,然后返回该页面。然后打开网络监视器选项卡(在 Firefox 中为 Ctrl+Shift+E),并再次单击 Export CSV。您应该会看到一个这样的 GET 请求

然后右键单击它>复制>复制为curl。这应该给你一个看起来像这样的请求

curl 'https://www.historique-meteo.net/site/export.php?ville_id=1025&annee=2016&mois=02' -H 'User-Agent: YourUA' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Connection: keep-alive' -H 'Referer: https://www.historique-meteo.net/france/rh-ne-alpes/annecy-haute-savoie/2016/02/' -H 'Cookie: __cfduid=UID; cf_chl_2=CHL; cf_chl_prog=x19; cf_clearance=CLR' -H 'Upgrade-Insecure-Requests: 1' -H 'Pragma: no-cache' -H 'Cache-Control: no-cache'

然后,您可以使用它从您的终端下载 CSV 文件,无论他们保存 cloudflare cookie 的时间有多长。使用-o output.csv 保存文件。这不是绕过 cloudflare,任何可疑活动都可能使 cookie 再次过期,但您可以做的另一件事是使用 selenium

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-21
    • 2015-02-20
    • 2015-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 2018-04-25
    相关资源
    最近更新 更多