【问题标题】:Want to get both tables from the web page想要从网页中获取两个表
【发布时间】:2020-07-14 21:07:21
【问题描述】:

我正在尝试通过使用类名来获取它。但没有得到任何回应。

代码如下:

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'))

我想从页面中获取这两个表: 这是第一张桌子

这是第二张表:

【问题讨论】:

    标签: python web-scraping beautifulsoup scrapy


    【解决方案1】:

    这些表没有类或 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
    

    【讨论】:

      【解决方案2】:

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

      tables = soup.find_all('table')
      

      现在您迭代一个 tables 变量来获取所需的内容。

      【讨论】:

        【解决方案3】:

        此页面中有超过 2 个表格。您通常需要检查代码以查看所需的表。

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

        在您的情况下,您似乎在关注tables[7]tables[11]

        【讨论】:

          猜你喜欢
          • 2015-12-11
          • 2020-07-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-07-09
          • 2016-11-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多