Created
December 13, 2012 21:33
-
-
Save geobabbler/4280156 to your computer and use it in GitHub Desktop.
Script to read Wordpress export file, fetch images via URL, and write locally.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import os | |
import urllib | |
from bs4 import BeautifulSoup | |
#TODO: pass in xml path and output folder as args | |
xml = open('C:\\Workspace\\blog\\geomusings_export.xml').read() | |
doc = BeautifulSoup(xml) | |
#attachments could technically be something other than images | |
#extract all attachment elements from Wordpress export file | |
for item in doc.findAll('wp:attachment_url'): | |
#Contens will be URL | |
lnk = item.contents[0] | |
#Get just the file name | |
fname = os.path.basename(lnk) | |
print(os.path.basename(lnk)) | |
#TODO: add some logic to handle duplicate file names | |
#Get file via URL and write locally | |
f = open('C:\\Workspace\\blog\\images\\' + fname,'wb') | |
f.write(urllib.urlopen(lnk).read()) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment