Last active
November 11, 2015 18:53
-
-
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.
This file contains hidden or 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 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