简体   繁体   English

美汤表转CSV

[英]Beautiful Soup Table to CSV

I am using beautiful soup to try and scrape a website table and extract only specific columns to a CSV file.我正在使用漂亮的汤来尝试抓取网站表并仅将特定列提取到 CSV 文件中。

import requests
import urllib.request
from bs4 import BeautifulSoup

product_table = browser.page_source

soup = BeautifulSoup(product_table, 'html.parser')

table = soup.find_all('table')[4]

table_rows = table.find_all('tr')
for tr in table_rows:
    td = tr.find_all('td')
    row = [i.text for i in td]
    print(row)

the print(row) outputs: print(row)输出:

[]
['', 'CANDY', 'ALBANESE CONFEC', 'Albanese Confectionery Group', 'Gummi Sour Bears 12 Flavor', '12', '7 oz', '17.14', 'CS', '53328', '', 'ACG53328', '', '\xa0\xa0\xa0\xa0']
['', 'CANDY', 'ALBANESE CONFEC', 'Albanese Confectionery Group', 'Gummi Bears 12 Flavor', '12', '7.5 oz', '17.14', 'CS', '53348', '', 'ACG53348', '', '\xa0\xa0\xa0\xa0']
['', 'CANDY', 'ALBANESE CONFEC', 'Albanese Confectionery Group', 'Gummi Mini Worms 12 Flavor', '12', '7.5 oz', '17.14', 'CS', '53350', '', 'ACG53350', '', '\xa0\xa0\xa0\xa0']
['', 'CANDY', 'ALBANESE CONFEC', "Albanese World's Best", 'Gummi Bears 12 Flavor', '6', '9 oz', '11.59', 'CS', '53380', '', 'ACG53380', '', '\xa0\xa0\xa0\xa0']
['', 'CANDY', 'ALBANESE CONFEC', "Albanese World's Best", 'Gummi Mini Worms 12 Flavor', '6', '9 oz', '11.59', 'CS', '53381', '', 'ACG53381', '', '\xa0\xa0\xa0\xa0']
['', 'CANDY', 'ALBANESE CONFEC', "Albanese World's Best", 'Peach Rings', '6', '8 oz', '11.59', 'CS', '53383', '', 'ACG53383', '', '\xa0\xa0\xa0\xa0']
['', 'CANDY', 'ALBANESE CONFEC', "Albanese World's Best", 'Gummi Worms Mini Sour Neon', '6', '8 oz', '11.59', 'CS', '53384', '', 'ACG53384', '', '\xa0\xa0\xa0\xa0']
['', 'CANDY', 'ALBANESE CONFEC', "Albanese World's Best", 'Gummi Bears 12 Flavor', '12', '3.5 oz', '8.23', 'CS', '53450', '', 'ACG53450', '', '\xa0\xa0\xa0\xa0']
['', 'CANDY', 'ALBANESE CONFEC', "Albanese World's Best", 'Gummi Sherbet Bears 12 Flavor', '12', '3.5 oz', '8.23', 'CS', '53456', '', 'ACG53456', '', '\xa0\xa0\xa0\xa0']
['', 'CANDY', 'AMERICAN LICORI', 'Red Vines', 'Red Vines Orig Red Twists Bag', '12', '8 oz', '19.20', 'CS', '00232', '', 'AML00232', '', '\xa0\xa0\xa0\xa0']

So my question is: How to I extract only cells [11] and [7] from each row and print all of them side by side to a csv.所以我的问题是:如何从每一行中仅提取单元格[11][7]并将它们并排打印到 csv。 So example for row 1 I want to write ACG53328(Cells A) and 17.14 (Cells B) to a csv file and continue downward.例如,对于第 1 行,我想将 ACG53328(单元格 A)和 17.14(单元格 B)写入 csv 文件并继续向下。 There are around 4,000 additional lines that I didn't paste here if that makes a difference.如果有什么不同的话,我没有在此处粘贴大约 4,000 条额外的行。

Something like the following should work:像下面这样的东西应该可以工作:

import csv
import requests
import urllib.request
from bs4 import BeautifulSoup

product_table = browser.page_source

soup = BeautifulSoup(product_table,'html.parser')
table = soup.find_all('table')[4]
with open('output.csv', 'w', newline="") as f:
    writer = csv.writer(f)
    writer.writerow(['SKU', 'TD_7'])
    for tr in table.find_all('tr'):
        try:
            td_12 = tr.find_all('td')[12].get_text(strip=True)
        except IndexError:
            td_12 = ""
        try:
            td_08 = tr.find_all('td')[8].get_text(strip=True)
        except IndexError:
            td_08 = ""
        writer.writerow([td_12, td_08])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM