使用javascript h从网页下载zip文件

2024-05-01 21:26:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我想创建一个python脚本,用于从以下网站下载zip文件:

https://www.histdata.com/download-free-forex-historical-data/?/ascii/1-minute-bar-quotes/eurusd/2018

我可以看到zip文件的链接。如果我点击它的zip文件下载开始。你知道吗

我看到页面中该文件的代码如下:

<a title="Download the zip data file" id="a_file" href="javascript:return true;" target="nullDisplay">HISTDATA_COM_ASCII_EURUSD_M1_2018.zip</a>

所以我没有直接的链接,我可以得到下载文件。你知道吗

为了获得链接列表,我使用了以下代码:

from bs4 import BeautifulSoup, SoupStrainer
import requests
import re

pageLink = "https://www.histdata.com/download-free-forex-historical-data/?/ascii/1-minute-bar-quotes/eurusd/2018"
data = requests.get(pageLink)
dataSoup = BeautifulSoup(data.text, features="html.parser")
linkPattern = re.compile(r".*\.zip.*")
for dataLink in dataSoup.find_all('a'):
  print("--- Downloading zip file: " + dataLink.get('href'))

通过这个我获得了页面的所有链接,但是zip链接没有出现。如果将dataSoup.find_all('a')替换为dataSoup.find_all('a', href=linkPattern),则列表为空。你知道吗

如何从脚本下载zip文件?你知道吗

编辑:

我已经看到了另一个问题,应该是这个重复,但我找不到办法使它工作。你知道吗

从链接中我可以看到脚本应该是"javascript:return true;",因此我创建了以下代码:

从bs4进口,SoupStrainer 从selenium导入webdriver 导入请求 进口re

浏览器=webdriver.PhantomJS文件()

pageLink = "https://www.histdata.com/download-free-forex-historical-data/?/ascii/1-minute-bar-quotes/eurusd/2018"
browser.get(pageLink)
browser.execute_script("javascript:return true;")

即使在这种情况下,它也不起作用,因此我无法通过锁定重复的问题来找到解决方案。你知道吗

编辑2:

我已经看到javascript没有任何功能。这个链接似乎有一个目标nullDisplay。当我单击链接时,似乎调用了目标,代码如下:

<form id="file_status" name="file_status" target="nullDisplay" method="POST" action="/getStatus.php">
        <input type="hidden" name="tk" id="tk" value="632ec105a874a64a1cf28120afa145e5" />
        <input type="hidden" name="date" id="date" value="2019" />
           <input type="hidden" name="datemonth" id="datemonth" value="201910" />
        <input type="hidden" name="platform" id="platform" value="ASCII" />
        <input type="hidden" name="timeframe" id="timeframe" value="M1" />
        <input type="hidden" name="fxpair" id="fxpair" value="EURUSD" />
    </form>

So the problem is how to recognize this post and how to use it for downloading data.

Tags: 文件代码nameidinputdatavalue链接