2016-04-28 9 views
0

私はPython 2.7.11を実行しています| Windows 10では、beautifulsoup4とlxmlを使用しています。Python HTMLページのタイトルから文字を再整列&削除する

import urllib2 
import re 
from bs4 import BeautifulSoup 

soup = BeautifulSoup(urllib2.urlopen("http://www.daisuki.net/us/en/anime/watch.GUNDAMUNICORNRE0096.13142.html"), "lxml") 
Name = soup.title.string 

print(Name.replace('#', "")) 

出力:

01 DEPARTURE 0096 - 機動戦士ガンダムユニコーンRE:0096 - だいすき

所望の出力:

機動戦士ガンダムユニコーンRE:0096 - 01 DEPARTURE 0096

最後に " - 大雪"を取り除いて文字列を再注文する方法はありますか?タイトルの

答えて

2

スプリット-によると並べ替えパーツ:受信

>>> import urllib2 
>>> from bs4 import BeautifulSoup 
>>> 
>>> soup = BeautifulSoup(urllib2.urlopen("http://www.daisuki.net/us/en/anime/watch.GUNDAMUNICORNRE0096.13142.html"), "lxml") 
>>> Name = soup.title.string 
>>> 
>>> " - ".join(Name.replace('#', "").split(" - ")[1::-1]) 
u'MOBILE SUIT GUNDAM UNICORN RE:0096 - 01 DEPARTURE 0096' 
2

ハックソリューション:

Name = "01 DEPARTURE 0096 - MOBILE SUIT GUNDAM UNICORN RE:0096 - DAISUKI" 
print ("- ".join(reversed(Name.split('-')[:2]))).strip() 
関連する問題