要从网页中获取两个表吗

2024-10-02 16:31:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图通过使用类名来获得它。但是没有得到任何回应

代码如下:

from requests import get
import requests
from bs4 import BeautifulSoup
import csv

url = 'https://www.hydraulichoses.com/sae100-r2s-2-wire-hydraulic-hoses-p/r2s.htm'

soup = BeautifulSoup( requests.get(url).content, 'lxml')

product_category = ''

product_category = soup.find('td', class_ = 'vCSS_breadcrumb_td').findAll('a')[1].text.strip()

product_title = soup.find('td', class_ = 'vCSS_breadcrumb_td').find('h1').text

print(soup.find('table', class_ = 'product-table'))

我想从页面中获取以下两个表: 这是第一张表enter image description here

这是第二张表:

enter image description here


Tags: fromimporturlgetfindproductrequestsclass
3条回答

这些表没有类或id,因此需要以其他方式搜索它们

例如:

import requests
from bs4 import BeautifulSoup


url = 'https://www.hydraulichoses.com/sae100-r2s-2-wire-hydraulic-hoses-p/r2s.htm'
soup = BeautifulSoup( requests.get(url).content, 'lxml')

first_table = soup.find('span', id='product_description').find_next('table')
second_table = soup.find('span', id='technical').find_next('table', width='98%')

# print them on screen:

for tr in first_table.select('tr'):
    print(*[td.get_text(strip=True, separator=' ') for td in tr.select('td, th')])

print('-' * 80)

for tr in second_table.select('tr'):
    print(*[td.get_text(strip=True, separator=' ') for td in tr.select('td, th')])

印刷品:

Item# Item Name Our Price Qty Add
R2S-04 100R2S-04  - 1/4" R2  Hydraulic Hose (per foot) $1.40  
R2S-04-REEL 100R2S-04-Reel  - 1/4" R2  Hydraulic Hose - 500 feet $670.36  
R2S-06 100R2S-06  - 3/8" R2  Hydraulic Hose (per foot) $1.67  
R2S-06-REEL 100R2S-06-Reel  - 3/8" R2  Hydraulic Hose - 450 feet $721.91  
R2S-08 100R2S-08  - 1/2" R2  Hydraulic Hose (per foot) $1.85  
R2S-08-REEL 100R2S-08-Reel  - 1/2" R2  Hydraulic Hose - 320 feet $567.22  
R2S-10 100R2S-10  - 5/8" R2  Hydraulic Hose (per foot) $2.61  
R2S-10-REEL 100R2S-10-Reel  - 5/8" R2  Hydraulic Hose - 520 feet $1,293.67  
R2S-12 100R2S-12  - 3/4" R2  Hydraulic Hose (per foot) $2.73  
R2S-12-REEL 100R2S-12-Reel  - 3/4" R2  Hydraulic Hose - 400 feet $1,050.18  
R2S-16 100R2S-16  - 1" R2  Hydraulic Hose (per foot) $3.75  
R2S-16-REEL 100R2S-16-Reel  - 1" R2  Hydraulic Hose - 200 feet $719.22  
R2S-20 100R2S-20  - 1-1/4" R2  Hydraulic Hose (per foot) $11.65  
R2S-20-REEL 100R2S-20-Reel  - 1-1/4" R2  Hydraulic Hose - 131 feet (apprx.) $1,466.28  
R2S-24 100R2S-24  - 1-1/2" R2  Hydraulic Hose (per foot) $13.56  
R2S-24-REEL 100R2S-24-Reel  - 1-1/2" R2  Hydraulic Hose - 131 feet $1,750.30  
R2S-32 100R2S-32  - 2" R2  Hydraulic Hose (per foot) $17.23  
R2S-32-REEL 100R2S-32-Reel  - 2" R2  Hydraulic Hose - 131 feet $2,173.96  
Check the items you wish to purchase, then click
                                        
Item # Inside Diameter Outside Diameter Working Pressure Burst Presure Bend Radius Reel Size
R2S-04 1/4" 0.59" 5,800 psi 20,000 psi 4" 500 feet
R2S-06 3/8" 0.75" 4,785psi 16,000 psi 5" 450 feet
R2S-08 1/2" 0.87" 3,990 psi 14,000 psi 7" 320 feet
R2S-10 5/8" 1.00" 3,625 psi 11,000 psi 8" 520 feet
R2S-12 3/4" 1.15" 3,120 psi 9,000 psi 9.5" 400 feet
R2S-16 1" 1.50" 2,395 psi 8,000 psi 12" 200 feet
R2S-20 1-1/4" 1.90" 1,815 psi 6,500 psi 16.5" 131 feet (apprx.)
R2S-24 1-1/2" 2.15" 1,250 psi 5,000 psi 19.5" 131 feet
R2S-32 2" 2.64" 1,125 psi 4,500 psi 25.5" 131 feet

您必须通过css selectorsoup中查找这两个表

tables = soup.find_all('table')

现在,您在一个tables变量中迭代以获取所需的内容

此页中有两个以上的表。您通常需要检查代码以查看需要哪些表

tables=soup.findAll("table")
print(len(tables))
#outputs: 12

在您的情况下,您似乎在tables[7]tables[11]之后

相关问题 更多 >