【发布时间】: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']
【问题讨论】:
-
底部不是桌子。请将所需的输出显示为实际表格并尽可能包含 url,否则请包含相关的 html。
-
@QHarr 我需要我在示例中给出的输出我尝试了很多但无法将其分开链接是grainger.com/category/power-transmission/bearings/ball-bearings/…
标签: python selenium-webdriver web-scraping beautifulsoup