简体   繁体   中英

Get filename of downloadable binary from php url without actually downloading the file

I'm doing some webscraping with Selenium in Python and I have a link on a page that points to, eg

<a href="/zip.php?zipid=103">Click Here To Download</a>

Now, of course, if I click on it, my browser will immediately start downloading the file, eg myinterestingarchive.zip

What I'm wondering is if I can inject some JavaScript, say, that will tell me the filename myinterestingarchive.zip WITHOUT my clicking the link, because I'd like to record the filename in my program's log, and it's nowhere in the source or OuterHTML, just that php url.

If it support HEAD request that will only download http headers you can do

import requests

......
# set the request with selenium cookies
cookies = {c['name']: c['value'] for c in driver.get_cookies()}
response = requests.head('http://....../zip.php?zipid=103', cookies=cookies )
print(response.headers['Content-Disposition'])
# attachment; filename=zip/myinterestingarchive.zip

And yes you can do this with injected JavaScript but it more simple using requests

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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