User:Wnt/Python script to grab multiple files
This is a crude python 2.7.13 script that was useful for downloading multiple files/pages from a site. The pages were specified one per line in input.txt, full URL for each (including http: or https:) - I was just using a spreadsheet to set up the multiple numbers. I wanted to keep this around in case I lose it before I need it again, and maybe it can help someone else. I did this in 2.7.13; it didn't work on 2.7.9 because the Heartbleed bug fix prevented a handshake with https. It doesn't work in Python 3.x because urllib2 was merged into urllib and apparently needs to be altered in some way more than just deleting the 2, which I didn't bother to figure out.
# -*- coding: utf-8 -*-
import sys, os, re, random, hashlib, hmac, logging, json, time, urllib2
file_base = os.path.dirname(__file__)
input_loc = os.path.join(file_base, 'input.txt')
try:
input_file = open(input_loc,'r')
print ('reading: ', input_loc, '\n')
snarf = input_file.read() # this should be a fairly short file!
urls = snarf.split('\n')
except:
sys.exit('Input file input.txt not found in program directory')
for url in urls:
time.sleep(0.5)
temp = url.split('/')
filename = temp[-1]
print ('filename is ',filename)
del temp[-1]
linkbase = '/'.join(temp)+'/'
print ('linkbase is ', linkbase)
output_loc = os.path.join(file_base, filename)
try:
print ('trying to open output')
output_file = open(output_loc,'wb')
except:
sys.exit('Failed to open output')
req = urllib2.Request(linkbase+filename, headers = {'User-Agent': 'Mozilla/5.0 (X11; U; Linux i686) Gecko/20071127 Firefox/2.0.0.11'})
response = urllib2.urlopen(req)
print ('tried to open url')
html = response.read()
print (len(html), ' characters read\n')
output_file.write(html)
output_file.close()
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.