Python:从javascript按钮获取下载链接

Python:从javascript按钮获取下载链接,javascript,python,url,download,Javascript,Python,Url,Download,我正试图让我的脚本从www.subscene.com下载字幕。问题是网页上的下载按钮是用java制作的,出于某种原因,即使提取URL,我也无法下载字幕 我想这是下载按钮的代码: <a id="s_lc_bcr_downloadLink" class="downloadLink rating0" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;s$lc$bcr$downl

我正试图让我的脚本从www.subscene.com下载字幕。问题是网页上的下载按钮是用java制作的,出于某种原因,即使提取URL,我也无法下载字幕

我想这是下载按钮的代码:

<a id="s_lc_bcr_downloadLink" class="downloadLink rating0" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;s$lc$bcr$downloadLink&quot;, &quot;&quot;, true, &quot;&quot;, &quot;/english/How-I-Met-Your-Mother-Seventh-Season/subtitle-482407-dlpath-90698/zip.zipx&quot;, false, true))">Download English Subtitle</a><a id="s_lc_bcr_previewLink" href="javascript:togglePreview(482407, 'zip');">(See preview)</a>
(增补)"http://subscene.com")

但由于某些原因,它没有下载正确的文件。我该怎么办

编辑:

非常感谢!不幸的是,我不能让它工作:(上面写着

from selenium import webdriver

browser = webdriver.Firefox()
browser.execute_script('WebForm_DoPostBackWithOptions(newWebForm_PostBackOptions("s$lc$bcr$downloadLink", "", true, "", "/english/How-I-Met-Your-Mother-Seventh-Season/subtitle-482407-dlpath-90698/zip.zipx", false, true))')

Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
browser.execute_script('WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("s$lc$bcr$downloadLink", "", true, "", "/english/How-I-Met-Your-Mother-Seventh-Season/subtitle-482407-dlpath-90698/zip.zipx", false, true))')
File "C:\Users\User\AppData\Roaming\Python\Python27\site-packages\selenium\webdriver\remote\webdriver.py", line 385, in execute_script{'script': script, 'args':converted_args})['value']
File "C:\Users\User\AppData\Roaming\Python\Python27\site-packages\selenium\webdriver\remote\webdriver.py", line 153, in execute
self.error_handler.check_response(response)
File "C:\Users\User\AppData\Roaming\Python\Python27\site-packages\selenium\webdriver\remote\errorhandler.py", line 126, in check_response
raise exception_class(message, screen, stacktrace) 
WebDriverException: Message: ''
从selenium导入webdriver
browser=webdriver.Firefox()
browser.execute_script('WebForm_DoPostBackWithOptions(新的WebForm_PostBackOptions(“s$lc$bcr$downloadLink)”,“”,true,“,“/english/How-I-Met-Your-Mother-Seven-Season/subtitle-482407-dlpath-90698/zip.zipx”,false,true)))
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
browser.execute_script('WebForm_DoPostBackWithOptions(新的WebForm_PostBackOptions(“s$lc$bcr$downloadLink)”,“”,true,“,“/english/How-I-Met-Your-Mother-Seven-Season/subtitle-482407-dlpath-90698/zip.zipx”,false,true)))
文件“C:\Users\User\AppData\Roaming\Python27\site packages\selenium\webdriver\remote\webdriver.py”,第385行,在执行脚本{'script':脚本,'args':转换的参数})['value']
文件“C:\Users\User\AppData\Roaming\Python27\site packages\selenium\webdriver\remote\webdriver.py”,第153行,执行
self.error\u handler.check\u响应(响应)
文件“C:\Users\User\AppData\Roaming\Python27\site packages\selenium\webdriver\remote\errorhandler.py”,第126行,在check\u响应中
引发异常类(消息、屏幕、堆栈跟踪)
WebDriverException:消息:“”

正如John所说,这不是文件,而是javascript代码。因此,您可以执行javascript,依次下载文件,而不是使用urllib.urlretrieve获取该文件。这可以使用selenium模块完成-

from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://subscene.com/english/How-I-Met-Your-Mother-Seventh-Season/subtitle-482407.aspx')        
browser.execute_script('WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("s$lc$bcr$downloadLink", "", true, "", "/english/How-I-Met-Your-Mother-Seventh-Season/subtitle-482407-dlpath-90698/zip.zipx", false, true))')
raw_input()

我使用Firebug获得了这个javascript片段。

您试图下载的(zip.zipx)不是文件,而是一些javascript。我正在研究如何获取下载的url。这将很难找到每个文件的实际url。似乎所有内容都是通过javascript从服务器检索的。我不认为这是一个url,然后可能是本地目录,你必须仔细看看这些站点的javascript以及它如何处理这些文件。我注意到
http://subscene.com/downloadissue.aspx?subtitleId=482407&contentType=zip
这意味着它找到
subtitid
,然后确保
zip
contentType
,然后从那里抓取它。这可能是用SQL的一种形式组织的。非常好@我想你也可以用mechanize python库获得类似的结果,但这已经足够优雅了。但是它不需要您同时安装selenium java服务器等吗?@alonisser谢谢,是的,您需要为Python安装selenium模块。使用PIP下载模块非常简单。很高兴有人能够帮助他+1。非常感谢,请查看我的下一篇帖子:)@TheHarst:我添加了一些代码来“让它工作”。希望你不介意。如果我做得不对,请纠正。
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://subscene.com/english/How-I-Met-Your-Mother-Seventh-Season/subtitle-482407.aspx')        
browser.execute_script('WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("s$lc$bcr$downloadLink", "", true, "", "/english/How-I-Met-Your-Mother-Seventh-Season/subtitle-482407-dlpath-90698/zip.zipx", false, true))')
raw_input()