【问题标题】:Unable to write data in a csv file in a customized manner无法以自定义方式将数据写入 csv 文件
【发布时间】:2018-06-11 16:01:48
【问题描述】:

我编写了一个脚本来从表中获取数据并将它们写入csv 文件中。所需的数据正在通过,我的脚本也可以将它们写入 csv 文件。但是,我无法解决的唯一问题是将所有数据放在单独的列中。我希望在不同的列中获得namelink,但它们在同一列中。如何解决?任何帮助将不胜感激。

我正在尝试的脚本:

import csv
from bs4 import BeautifulSoup

content="""
<tr>
    <td align="center">1964</td>
    <td><span class="sortkey">Townes, Charles Hard</span><span class="vcard"><span class="fn"><a href="/wiki/Charles_Hard_Townes" class="mw-redirect" title="Charles Hard Townes">Charles Hard Townes</a></span></span>;<br>
    <span class="sortkey">Basov, Nikolay</span><span class="vcard"><span class="fn"><a href="/wiki/Nikolay_Basov" title="Nikolay Basov">Nikolay Basov</a></span></span>;<br>
    <span class="sortkey">Prokhorov, Alexander</span><span class="vcard"><span class="fn"><a href="/wiki/Alexander_Prokhorov" title="Alexander Prokhorov">Alexander Prokhorov</a></span></span></td>
    <td><span class="sortkey">Hodgkin, Dorothy</span><span class="vcard"><span class="fn"><a href="/wiki/Dorothy_Hodgkin" title="Dorothy Hodgkin">Dorothy Hodgkin</a></span></span></td>
    <td><span class="sortkey">Bloch, Konrad Emil</span><span class="vcard"><span class="fn"><a href="/wiki/Konrad_Emil_Bloch" title="Konrad Emil Bloch">Konrad Emil Bloch</a></span></span>;<br>
    <span class="sortkey">Lynen, Feodor Felix Konrad</span><span class="vcard"><span class="fn"><a href="/wiki/Feodor_Felix_Konrad_Lynen" class="mw-redirect" title="Feodor Felix Konrad Lynen">Feodor Felix Konrad Lynen</a></span></span></td>
    <td><span class="sortkey">Sartre, Jean-Paul</span><span class="vcard"><span class="fn"><a href="/wiki/Jean-Paul_Sartre" title="Jean-Paul Sartre">Jean-Paul Sartre</a></span></span><sup class="reference" id="ref_Note1D"><a href="#endnote_Note1D">[D]</a></sup></td>
    <td><span class="sortkey">King, Jr., Martin Luther</span><span class="vcard"><span class="fn"><a href="/wiki/Martin_Luther_King,_Jr." class="mw-redirect" title="Martin Luther King, Jr.">Martin Luther King, Jr.</a></span></span></td>
    <td align="center">—</td>
</tr>
"""
soup = BeautifulSoup(content,"lxml")
for items in soup.select('tr'):
    item_name = [' '.join([item.text,item.get('href')]) for item in items.select(".fn a")]
    print(item_name)
    with open("tab_data.csv","a",newline="") as infile:
        writer = csv.writer(infile)
        writer.writerow(item_name)

我得到的输出(名称和链接在同一列中): 我想要的输出(名称和链接在单独的列中):

顺便说一句,这是这个帖子的后续问题:Thread_Link

【问题讨论】:

    标签: python python-3.x csv web-scraping beautifulsoup


    【解决方案1】:

    如果您需要不同列中的文本和网址,那么您不必加入它们:

    import itertools
    ...
    for items in soup.select('tr'):
        list_of_tuples = [(item.text,item.get('href')) for item in items.select(".fn a")]
        item_name = list(itertools.chain(*list_of_tuples))
        print(item_name)
        with open("tab_data.csv","a",newline="") as infile:
            writer = csv.writer(infile)
            writer.writerow(item_name)
    

    编辑 OP问*list_of_tuples是什么意思

    首先,我们需要了解itertools.chain( x, y ) 的含义。这旨在“链接”两个列表(可枚举):

    >>> import itertools
    >>> x=[1,2,3]
    >>> y=(4,5,6)
    >>> itertools.chain( x, y )
    <itertools.chain object at 0x7f5811df8690>
    >>> list(itertools.chain( x, y ))
    [1, 2, 3, 4, 5, 6]
    

    现在,我们准备好了解unpacking arguments。假设我们将 x 和 y 参数(来自示例)放入一个列表中:l = [x, y]。在这种情况下,我们可以使用* 操作符解压这个列表:

    >>> l=[x,y]
    >>> list(itertools.chain( *l ))
    [1, 2, 3, 4, 5, 6]
    

    在您的情况下,您有很多要链接的元组:

    >>> t1=(1,2)
    >>> t2=(3,4)
    >>> t3=(4,5)
    >>> list(itertools.chain( t1, t2, t3 ))
    [1, 2, 3, 4, 4, 5]
    

    但是您在要解包的列表中有这个元组:

    >>> l=[t1, t2, t3]
    >>> list(itertools.chain( *l ))
    [1, 2, 3, 4, 4, 5]
    

    我希望这对你有意义。

    【讨论】:

    • 哇!!就是这个。它完成了这项工作。我有点难以理解*list_of_tuples 是星号。您愿意对此提供一点解释吗?感谢您的解决方案。
    • @Topto,在回答中进行了解释。
    • 感谢一万亿@danihp。现在,我明白了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-28
    • 2017-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多