2017-04-11 138 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换行字符在一年之后。但在电影片名之后仍然会出现换行符。生病贴上我的代码anwyway感谢!:

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) 

输出是这样的:

[ '肖申克的救赎\ n'],[ '教父\ n'],['教父:第二部分\ n'],['黑暗骑士\ n'],['12 Angry Men \ 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