我在使用 BeautifulSoup 提取的网站上有下表
这是网址(我还附上了一张图片 python - 获取 BeautifulSoup 中表格的内容-LMLPHP

理想情况下,我希望每个公司都在 csv 中的一行中,但是我将其放在不同的行中。请看附图。

python - 获取 BeautifulSoup 中表格的内容-LMLPHP

我希望它像在字段“D”中一样,但我在 A1、A2、A3 中得到它......

这是我用来提取的代码:

def _writeInCSV(text):
    print "Writing in CSV File"
    with open('sara.csv', 'wb') as csvfile:
        #spamwriter = csv.writer(csvfile, delimiter='\t',quotechar='\n', quoting=csv.QUOTE_MINIMAL)
        spamwriter = csv.writer(csvfile, delimiter='\t',quotechar="\n")

        for item in text:
            spamwriter.writerow([item])

read_list=[]
initial_list=[]


url="http://www.nse.com.ng/Issuers-section/corporate-disclosures/corporate-actions/closure-of-register"
r=requests.get(url)
soup = BeautifulSoup(r._content, "html.parser")

#gdata_even=soup.find_all("td", {"class":"ms-rteTableEvenRow-3"})

gdata_even=soup.find_all("td", {"class":"ms-rteTable-default"})




for item in gdata_even:
    print item.text.encode("utf-8")
    initial_list.append(item.text.encode("utf-8"))
    print ""

_writeInCSV(initial_list)

有人可以帮忙吗?

最佳答案

这是一个想法:

  • 从表中读取标题单元格
  • 从表中读取所有其他行
  • 压缩所有带有标题的数据行单元格,生成字典列表
  • 使用 csv.DictWriter() 转储到 csv

  • 执行:
    import csv
    from pprint import pprint
    
    from bs4 import BeautifulSoup
    import requests
    
    url = "http://www.nse.com.ng/Issuers-section/corporate-disclosures/corporate-actions/closure-of-register"
    soup = BeautifulSoup(requests.get(url).content, "html.parser")
    
    rows = soup.select("table.ms-rteTable-default tr")
    headers = [header.get_text(strip=True).encode("utf-8") for header in rows[0].find_all("td")]
    
    data = [dict(zip(headers, [cell.get_text(strip=True).encode("utf-8") for cell in row.find_all("td")]))
            for row in rows[1:]]
    
    # see what the data looks like at this point
    pprint(data)
    
    with open('sara.csv', 'wb') as csvfile:
        spamwriter = csv.DictWriter(csvfile, headers, delimiter='\t', quotechar="\n")
    
        for row in data:
            spamwriter.writerow(row)
    

    关于python - 获取 BeautifulSoup 中表格的内容,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32434378/

    10-12 00:06
    查看更多