【问题标题】:How to seperate a list into table formate using python如何使用python将列表分成表格格式
【发布时间】:2021-02-07 04:38:41
【问题描述】:

我已经从网站上的多个表格中抓取数据到列表格式中,请帮助我将列表分成表格格式。我试过 split() 但没用。

from bs4 import BeautifulSoup
from selenium import webdriver
import time

class ProductTracker:

    def __init__(self, url):
        self.url = url
        self.user_agent = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36'}
        # self.responce = requests.get(url=self.url, headers= self.user_agent).text
        driver = webdriver.Chrome()
        driver.get(url)
        time.sleep(5)
        self.responce = driver.page_source
        driver.close()
        self.soup = BeautifulSoup(self.responce, 'lxml')

    def product_header(self):
        # for tabletitle in self.soup.findAll('h3', attrs={'class': 'search-table-view__heading'}).text:
        tabletitles = self.soup.find_all('h3', {'class': 'search-table-view__heading'})
        table_titles_list = []
        for title in tabletitles:
            table_titles_list.append(title.text)
        return table_titles_list
        return "Tag Not Found"

    def product_tableheader(self):
        tableheaders = self.soup.find_all('th',
                                          class_=lambda value: value and value.startswith("search-table-view__cell"))
        header_lst = []
        for tableheader in tableheaders:
            try:
                header_lst.append(tableheader.div.a.span.text.strip())
            except:
                try:
                    header_lst.append(tableheader.div.text.strip())
                except:
                    pass
        return header_lst
        return "Tag Not Found"

    def product_tablevalues(self):
        tablevalues = self.soup.find_all('tr', class_=lambda value: value and value.startswith(
            "search-table-view__web-parent-table-row"))
        values_lst = []
        for tablevalue in tablevalues:
            td_lst = tablevalue.find_all('td', class_=lambda value: value and value.startswith(
                "search-table-view__web-parent-table-row-cell"))
            for td in td_lst:
                try:
                    values_lst.append(td.text.strip().replace("\n", "").replace("\r", "").replace("                                                   "," ").replace("                                           "," "))
                except:
                    pass
        return values_lst
        return "Tag Not Found"
material = ProductTracker(url = "https://www.grainger.com/category/power-transmission/bearings/ball-bearings/radial-ball-bearings")
print(material.product_header())
print(material.product_tableheader())
print(material.product_tablevalues())

以下是我的输出,我从输出中删除了一些数据:

['NTN Single Row Radial Ball Bearings, Metric Series', 'BL Single Row Radial Ball Bearings, Metric Series', 'BL Single Row Radial Ball Bearings, Inch Series', 'DAYTON Single Row Radial Ball Bearings, Metric Series',........'TORRINGTON BEARINGS Single Row Radial Ball Bearings, Metric Series']
['Bore Dia.', 'Outside Dia.', 'Width', 'Seal/Shield Type', 'Item #', 'Price', 'Bore Dia.', 'Outside Dia.', 'Width', 'Seal/Shield Type', 'Item #', 'Price', 'Bore Dia.', 'Outside Dia.', 'Width', 'Seal/Shield Type', 'Item #', 'Price']
['4 mm', '13 mm', '5 mm', 'Double Shielded', '5U557', 'Regular Price $15.22 / each', '5 mm', '16 mm', '5 mm', 'Double Shielded', '5U592', 'Regular Price $10.10 / each', '6 mm', '19 mm', '6 mm', 'Select Seal/Shield Type', 'Multiple Items', '$9.10 - $14.65', '7 mm', '19 mm', '6 mm', 'Select Seal/Shield Type', 'Multiple Items', '$5.75 - $9.90', '7 mm', '22 mm', '7 mm', 'Select Seal/Shield Type', 'Multiple Items', '$8.50 - $15.05', '8 mm', '22 mm', '7 mm', 'Select Seal/Shield Type', 'Multiple Items', '$8.15, '9 mm', '24 mm', '7 mm', 'Double Shielded', '5U530', 'Regular Price $8.65 / each', '0.4375 in', '1.125 in', .............'0.375 in', 'Select Seal/Shield Type', 'Multiple Items', '$13.25 - $13.85', '0.4375 in', '0.9062 in', '0.3125 in', 'Select Seal/Shield Type', 'Multiple Items', '$11.25 - $11.80']

我必须拆分我的列表并像表格一样更改:

['NTN Single Row Radial Ball Bearings, Metric Series']

['Bore Dia.', 'Outside Dia.', 'Width', 'Seal/Shield Type', 'Item #', 'Price']
['4 mm','3 mm','5 mm','Double Shielded','5U557','Regular Price$15.22 / each']
['5 mm','16 mm','5 mm','Double Shielded','5U592','Regular Price$10.10 / each']

['BL Single Row Radial Ball Bearings, Metric Series']

['Bore Dia.', 'Outside Dia.', 'Width', 'Seal/Shield Type', 'Item #', 'Price']
['10 mm','15 mm','12 mm','Double Shielded','5U559','Regular Price$16.22 / each']
['54 mm','21 mm','9 mm','Double Shielded','5U598','Regular Price$10.10 / each']

【问题讨论】:

标签: python selenium-webdriver web-scraping beautifulsoup


【解决方案1】:

也许是这样的?

import re
from bs4 import BeautifulSoup as bs

soup = bs(driver.page_source, 'lxml')

for section in soup.select('.search-table-view__web-parent'):
    title = [section.select_one('.search-table-view__heading').text]
    header_row = [i.text.strip() for i in section.select_one(' .search-table-view__web-parent-table-head-row').select('th')][:-1]

    print(title)
    print()
    print(header_row)

    for row in section.select('.search-table-view__web-parent-table-row'):
        print([re.sub('\n+|\s{2,}',' ',r.text.strip()) for r in row.select('td')][:-1])
    
    print()

因为您需要循环部分可能需要重写以包含允许这样做的调用?

from bs4 import BeautifulSoup
from selenium import webdriver
import time, re

class ProductTracker:

    def __init__(self, url):
        self.url = url
        self.user_agent = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36'}
        # self.responce = requests.get(url=self.url, headers= self.user_agent).text
        driver = webdriver.Chrome()
        driver.get(url)
        time.sleep(5)
        self.response = driver.page_source
        driver.close()
        self.soup = BeautifulSoup(self.response, 'lxml')
    
    def loop_sections(self):
        try:
            for section in self.soup.select('.search-table-view__web-parent'):
                print(self.product_header(section))
                print()
                print(self.product_tableheader(section))
                self.product_tablevalues(section) # you might change this from a list of lists to print during as loop rather than nested list comprehension
                print()
        except Exception as e:
            print("error", e)
            return
        
    def product_header(self, section):
        try:
            table_title = [section.select_one('.search-table-view__heading').text]
            return table_title
        except Exception as e:
            print("error", e)
            return

    def product_tableheader(self, section):
        try:
            tableheaders = [i.text.strip() for i in section.select_one(' .search-table-view__web-parent-table-head-row').select('th')][:-1]
            return tableheaders
        except Exception as e:
            print("error", e)
            return
    
    def product_tablevalues(self, section):
        try:
            for row in section.select('.search-table-view__web-parent-table-row'):
                print([re.sub('\n+|\s{2,}',' ',r.text.strip()) for r in row.select('td')][:-1])
            return
        except Exception as e:
            print("error", e)
            return
        
material = ProductTracker(url = "https://www.grainger.com/category/power-transmission/bearings/ball-bearings/radial-ball-bearings")
material.loop_sections()

【讨论】:

  • 对不起 QHarr 先生,您的输出与我的示例输出不同
  • 能否请您帮我获取示例输出
  • 修订是否更接近要求?
  • 我尝试将此输出写入 excel,但出现属性错误,请您帮我...
  • 嗨,请随时提出一个新问题。抱歉,我之前没有看到这条评论。
猜你喜欢
  • 2016-05-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-03
  • 1970-01-01
  • 2019-10-28
  • 1970-01-01
  • 1970-01-01
  • 2016-12-26
相关资源
最近更新 更多