2017-04-11 12 views
0

['、\ n \ xa0文字と年(1994年)をリスト内のこのエントリから削除し、各エントリに対してこれを行うリストを反復処理する必要があります。 私はこれを行う方法がありますか?私は、Pythonの新しめだとエントリがとても似ている時間リスト内のエントリから特定の文字を削除する

のためにしようとしている:

[['The Shawshank Redemption\n(1994)\n\n\n 9.2\xa0\xa0\n\n'], ['The Godfather\n(1972)\n\n\n 9.2\xa0\xa0\n\n'], ['The Godfather: Part II\n(1974)\n\n\n 9.0\xa0\xa0\n\n'], 

編集:コードを含んでいないため申し訳ありませんが、IVは数字と\ nは改行文字を削除するために管理しました年後にまだ映画のタイトルの直後に改行文字を取得しています。

[[ 'ショーシャンクの空にの\ nの']、[ 'ゴッドファーザー\ N']、[ 'ゴッドファーザー:病気

from bs4 import BeautifulSoup 
import requests 
import random 

names = [] 
newList = [] 
url = 'http://m.imdb.com/chart/top' 
# get contents from url 
content = requests.get(url).content 
# get soup 
soup = BeautifulSoup(content,'lxml') # choose lxml parser 
# find all the references 
ref_tags = soup.findAll('span', { 'class' : 'media-body' }) 
realTags = soup.find_all("h4") 
# iterate through the ResultSet 
for i,ref_tag in enumerate(ref_tags): 
    # print text only 
    names.append('[{0}] {1}'.format(i,ref_tag.text)) 
pos = 0 
for name in names: 
    newName = names[pos] 
    newName = newName[9:] 
    newName = newName[:100] 
    newName = newName.split("(") 
    newName = newName[::2] 
    del newName[2:9:3] 
    newList.append(newName) 
    pos = pos + 1 

print(newList) 
choice = random.choice(newList) 
print(choice) 

!:おかげで、出力は次のようである私のコードanwywayを貼り付けパート2 \ n ']、['ダークナイト\ n ']、[12人の怒っている人\ n]

+2

? – nbro

+0

@nbroには、[最小限の、完全で検証可能な例](https://stackoverflow.com/help/mcve)を追加する必要があります。 – geostocker

答えて

0

だから私はどのように出力したいのですか?何でもありがとう!

相続人は、将来的にそれを必要とするかもしれない人のためのコード:

from bs4 import BeautifulSoup 
import requests 
import random 

names = [] 
newList = [] 
url = 'http://m.imdb.com/chart/top' 
# get contents from url 
content = requests.get(url).content 
# get soup 
soup = BeautifulSoup(content,'lxml') # choose lxml parser 
# find all the references 
ref_tags = soup.findAll('span', { 'class' : 'media-body' }) 
realTags = soup.find_all("h4") 
# iterate through the ResultSet 
for i,ref_tag in enumerate(ref_tags): 
    # print text only 
    names.append('[{0}] {1}'.format(i,ref_tag.text)) 
pos = 0 
for name in names: 
    newName = names[pos] 
    newName = newName[9:] 
    newName = newName[:100] 
    newName = newName.split("(") 
    newName = newName[::2] 
    del newName[2:9:3] 
    newList.append(newName) 
    pos = pos + 1 

wordChoice = random.choice(newList) 
str = str(wordChoice) 
editWord = str.split("\\n") 



print(editWord[1]) 

、出力はそうのようなものです:あなたが働かなかったことを正確にしようとしている何

Shutter Island 
関連する問題