簡體   English   中英

如何讓Beautifulsoup將表格中的串行HTML列表解析為CSV數據模式?

[英]How do I get Beautifulsoup to Parse a Serial HTML list in a table into a CSV pattern of data?

我有一個內部公司網頁,列出了長列表中的各種數據,我希望將其轉換為CSV文件進行審核。 數據格式為:

*CUSTOMER_1*
Email Link   Category_Text    Phone_Numbers
Email Link   Category_Text    Phone_Numbers
*Customer_2*
Email Link   Category_Text    Phone_Numbers
Email Link   Category_Text    Phone_Numbers

在HTML中編碼看起來像

<table id="responsibility">
    <tr class="customer">
        <td colspan="6">
            <strong>CUSTOMER 1</strong>
        </td>
    </tr>
    <tr id="tr_1" title="Role_Name1">
        <td><a href="email@company.com1">Name_1</a></td>
        <td>Category_Text</td>
        <td>Phone_Numbers</td>
        <td></td>
    </tr>
    <tr id="tr_2" title="Role_Name2">
        <td><a href="email@company.com2">Name_2</a></td>
        <td>Category_Text</td>
        <td>Phone_Numbers</td>
        <td></td>
    </tr>
    <tr class="customer">
        <td colspan="6">
            <strong>CUSTOMER 2</strong>
        </td>
    </tr>
    <tr id="tr_1" title="Role_Name1">
        <td><a href="email@company.com3">Name_3</a></td>
        <td>Category_Text</td>
        <td>Phone_Numbers</td>
        <td></td>
    </tr>
    <tr id="tr_2" title="Role_Name2">
        <td><a href="email@company.com2">Name_2</a></td>
        <td>Category_Text</td>
        <td>Phone_Numbers</td>
        <td></td>
    </tr>
</table>

我想以一種包含這種方式信息的file.csv結束

   CUSTOMER1,Role_Name1,Name_1,Email_1,Category_Text,Phone_Numbers
   CUSTOMER1,Role_Name2,Name_2,Email_2,Category_Text,Phone_Numbers
   CUSTOMER2,Role_Name1,Name_3,Email_3,Category_Text,Phone_Numbers
   CUSTOMER2,Role_Name1,Name_2,Email_2,Category_Text,Phone_Numbers

現在我可以獲得所有客戶名稱的列表或所有文本的列表,但我無法弄清楚如何迭代每個客戶,然后迭代每個客戶的每一行

from bs4 import BeautifulSoup

soup = BeautifulSoup(open("source.html"), "html.parser")

with open("output.csv",'w') as file:
    responsibility=soup.find('table',{'id':'responsibility'})
    line=responsibility.tr
    for i in responsibility:
        print(line)
        line=responsibility.tr.next_sibling

我希望這可以打印文檔中的每個標記,但它只打印第一個標簽,而不會循環到下一個標簽。

專注於這行代碼:

line=responsibility.tr

在這里,您使用.tr標記,它定位<tr>標記塊的第一個實例並返回它的內容。

這在這里意味着什么? 假設你有n<tr>標記的實例,那么使用.tr將只給你那些n <tr>實例中的第一個實例。 所以,如果你想提取所有n個,那么使用find_all() 它將返回所有可能匹配的列表。

line=responsibility.find_all("tr", class_="customer")

另外,添加class_="customer"過濾器。 它將幫助您找到具有“customer”類的所有<tr>塊。 然后只需使用.next_sibling就可以找到帶有title="Role_Name*"屬性的2個后續行。

因此,要將上述理論付諸實踐,請注意:

from bs4 import BeautifulSoup

soup = BeautifulSoup(open("source.html"), "html.parser")

with open("output.csv",'w') as file:
    responsibility=soup.find('table',{'id':'responsibility'})
    lines=responsibility.find_all("tr", class_ = "customer")
    for i in responsibility:
        for line in lines:
            line1=line.next_sibling              #locates tr with title="Role_Name1"
            line2=line.next_sibling.next_sibling #locates tr with title="Role_Name2"
            print(line1)
            print(line2)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM