从列表中删除字符串 unicode 标记并将每个项目放在单独的行中

Removing string unicode marks from list and having each item on separate line

我有一个列表,listOfActors,其中包含来自本网站上找到的数据的每部电影中演员的子列表 - http://www.boxofficemojo.com/yearly/chart/?yr=2013&p=.htm

我使用网络爬虫功能获取演员

def getActors(item_url):
    response = requests.get(item_url)
    soup = BeautifulSoup(response.content, "lxml")  # or BeautifulSoup(response.content, "html5lib")
    tempActors = []
    try:
        tempActors.append(soup.find(text="Actors:").find_parent("tr").find_all(text=True)[1:])
    except AttributeError:
        tempActors.append("n/a")
    return tempActors

结果是此格式的演员列表

[u'Jennifer Lawrence', u'Josh Hutcherson', u'Liam Hemsworth', u'Elizabeth Banks', u'Stanley Tucci', u'Woody Harrelson', u'Philip Seymour Hoffman', u'Jeffrey Wright', u'Jena Malone', u'Amanda Plummer', u'Sam Claflin', u'Donald Sutherland', u'Lenny Kravitz']
[u'Robert Downey, Jr.', u'Gwyneth Paltrow', u'Don Cheadle', u'Guy Pearce', u'Rebecca Hall', u'James Badge Dale', u'Jon Favreau', u'Ben Kingsley', u'Paul Bettany*', u' ', u'(Voice)', u'Mark Ruffalo*', u' ', u'(Cameo)']

我将这些数据导出到一个 csv 文件中,每个列表都在单独的一行中。我有两个问题:

首先,如何从每个子列表中删除 "u'" 标记,最好还有括号?

其次,当我打开 csv 文件时,我希望每个演员都在 excel 中自己的块中。现在他们都在一个巨大的街区。

I want the final output to be like:

Jennifer Lawrence |Josh Hutcherson|Liam Hemsworth|... so on and so forth

Robert Downey, Jr. | Gwyneth Paltrow|Don Cheadle|

而不是

[u'Jennifer Lawrence', u'Josh Hutcherson', u'Liam Hemsworth', u'Elizabeth Banks', u'Stanley Tucci', u'Woody Harrelson', u'Philip Seymour Hoffman', u'Jeffrey Wright', u'Jena Malone', u'Amanda Plummer', u'Sam Claflin', u'Donald Sutherland', u'Lenny Kravitz']

[u'Robert Downey, Jr.', u'Gwyneth Paltrow', u'Don Cheadle', u'Guy Pearce', u'Rebecca Hall', u'James Badge Dale', u'Jon Favreau', u'Ben Kingsley', u'Paul Bettany*', u' ', u'(Voice)', u'Mark Ruffalo*', u' ', u'(Cameo)']

这是我调用 getActors 函数的主要网络爬虫函数:

def spider(max_pages):
    page = 1
    while page <= max_pages:
        url = 'http://www.boxofficemojo.com/yearly/chart/?page=' + str(page) + '&view=releasedate&view2=domestic&yr=2013&p=.htm'
        source_code = requests.get(url)
        plain_text = source_code.text
        soup = BeautifulSoup(plain_text)
        for link in soup.select('td > b > font > a[href^=/movies/?]'):
             href = 'http://www.boxofficemojo.com' + link.get('href')
            listOfActors.append(getActors(href))
        page += 1

首先,您应该将当前的 getActors 实现更改为此。您当前的实现 returns 列表列表。这 returns 一个列表。

def getActors(item_url):
    response = requests.get(item_url)
    soup = BeautifulSoup(response.content, "lxml")  # or BeautifulSoup(response.content, "html5lib")
    tempActors = []
    try:
        return(soup.find(text="Actors:").find_parent("tr").find_all(text=True)[1:])
    except AttributeError:
        return ['n/a']

然后,在将 getActors 中的多个列表收集到一个名为 listOfActors 的列表列表后,您可以将它们全部写入一个 csv 文件,如下所示

out = open('csv.csv','w')
for i in listOfActors:
    line = ''
    for j in i:
        line = line+j+','
    out.write(line+'\n')

out.close()

请使用逗号分隔值。此外,python 将自动处理 unicode 字符串。