Skip to content

Instantly share code, notes, and snippets.

@subhaze
Last active November 11, 2015 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save subhaze/32914dec3886ca39571a to your computer and use it in GitHub Desktop.
Save subhaze/32914dec3886ca39571a to your computer and use it in GitHub Desktop.
I couldn't find any concrete way of selectively pulling content from an old Wordpress blog (with attachments). So... this python script will take an xml export file of all your Wordpress content and only move over the attachments that are found based on your selective content Wordpress export.
import codecs
from bs4 import BeautifulSoup
soup_all = BeautifulSoup(open('Wordpress_all_content.xml'))
soup_all_items = soup_all.find_all('item')
soup_posts = BeautifulSoup(open('Wordpress_selective_content.xml'))
soup_posts_items = soup_posts.channel.find_all('item')
def match_post(item):
for elem in soup_posts_items:
if elem.find('wp:post_id').text == item.find('wp:post_parent').text:
return True
return False
for elem in soup_all_items:
if elem.find('wp:post_type').text == 'attachment' and match_post(elem):
soup_posts.channel.append(elem)
with codecs.open('new_wp_import.xml', 'w', 'UTF-8') as file:
file.write(unicode(str(soup_posts), 'UTF-8'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment