【问题标题】:python BeautifulSoup not getting text from webpagepython BeautifulSoup没有从网页获取文本
【发布时间】:2020-05-14 00:07:35
【问题描述】:

我正在尝试使用 python 从网页中获取产品名称。但它只返回一个空标签。我还尝试了requests 库和lxml 解析BeautifulSoup。请帮我解决这个问题,在此先感谢:-)

网站中的 HTML:

<div class="product-name">SWAN</div>
   <div class="product-price">
   <span class="final-price">₹10650</span>
</div>
<div class="specification">
   <div>Specifications</div>
   <table>
      <tr>
         <td>....</td>
      </tr>
      <tr>
         <td>....</td>
     </tr>
   </table>
</div>

python 代码:

url = "http://opor.in/ProductDetail/Index?ProductId=212"
page = urlopen(url).read()
html = bs(page, 'html.parser')
model_name = html.find('div', attrs={'class':'product-name'})
spec = html.find('div', attrs={'class':'specification'})
print(model_name)
print(spec)

输出:

<div class="product-name"></div>
<div class="specification">
<div>Specifications</div>
<table></table>
</div>

【问题讨论】:

    标签: javascript python html web-scraping beautifulsoup


    【解决方案1】:

    java-scripts加载的数据。但是如果你在script标签中看到可用的DOM数据。从script标签中获取值并加载到json中然后获取key值。

    代码

    from urllib.request import urlopen
    from bs4 import BeautifulSoup as bs
    import json
    url = "http://opor.in/ProductDetail/Index?ProductId=212"
    page = urlopen(url).read()
    soup = bs(page, 'html.parser')
    
    for item in soup.find_all('script'):
       if 'productDetail' in item.text:
           data=item.text.split('var productDetail =')[-1].split('};')[0] + "}"
           datajson=json.loads(data.strip())
           print('Product Code :' + datajson['ProductCode'])
           for item in datajson['ProductSpecification']:
               print(item['SpecificationName'] + " : "+ item['SpecificationValue'])
    

    输出

    Product Code :1601KFMB
    MEMBRANE : MEMBRELLA -ALPHA- 80 GPD (2 NOS)
    PUMP : KEMFLO 48 V
    APPLICATION : SUITABLE FOR BRACKISH WATER
    FILTER LIFE : APPROX 3000 LITRE / 6 MONTHS
    FILTERS : SEDIMENT, PRECARBON, POST CARBON
    FLOAT : MEMBRELLA
    FR : MEMBRELLA /KFL
    INLINE SET : MEMBRELLA
    INPUT VOLTAGE : 100-300 VOLT AC (50Hz)
    INSTALLATION : COUNTER TOP
    MAX.OPERATION TDS : 4000 PPM
    MEMBRANE TYPE : THIN FILM COMPOSITE
    MIN.INLET PRESSURE / TEMP : 0.3 kg / cm2, 10 °C
    MODEL : WHALE 25
    OPERATING VOLTAGE : 48 VOLT (DC)
    PRODUCT DIMENSION : 21.1 (H) x 9.9 (W) x 16.7 (L)
    PURIFICATION CAPACITY : 25 LITRES PER HOUR
    RECOVERY RATE : MORE THAN 30% AT 27°c ± 2°c
    SMPS : MEMBRELLA / EQUALIANT
    SOLENOID VALVE : MEMBRELLA / SLX
    STORAGE CAPACITY : 20 LITRES
    TECHNOLOGY : REVERSE OSMOSIS SYSTEM
    TOTAL POWER CONSUMPTION : 50 W
    TUBE 1/4 : 5 METERS
    TUBE 3/8 : 2 METERS
    WEIGHT : 18 kg (Approx)
    WARRENTY &  SUPPORT : Since Whale  designs its purifiers and many of its parts  are a truly integrated system. Dealer only  can provide one-stop service ,guaranty and support for any service and maintenance, so most issues can be resolved in a single visit
    

    【讨论】:

      【解决方案2】:

      您正在搜索的数据实际上是由 javascript 加载的。您必须使用诸如 selenium 之类的包来检索数据。

      你可以试试这个:

      代码:

      from bs4 import BeautifulSoup as bs
      from selenium import webdriver
      import requests
      from selenium.webdriver.firefox.options import Options as FirefoxOptions
      
      # Use options to have your selenium headless
      options = FirefoxOptions()
      options.add_argument("--headless")
      driver = webdriver.Firefox(options=options)
      
      url = "http://opor.in/ProductDetail/Index?ProductId=212"
      driver.get(url)
      
      page = driver.page_source
      html = bs(page, 'html.parser')
      
      model_name = html.find('div', {'class':'product-name'})
      spec = html.find('div', {'class':'specification'})
      print(model_name)
      print(spec)
      

      结果:

      <div class="product-name">WHALE 25 LPH</div>
      <div class="specification">
      <div>Specifications</div>
      <table><tr><td class="specification-group" colspan="2"><div>General</div></td></tr><tr><td>Product Code</td><td>1601KFMB</td></tr><tr><td>MEMBRANE</td><td>MEMBRELLA -ALPHA- 80 GPD (2 NOS)</td></tr><tr><td>PUMP</td><td>KEMFLO 48 V</td></tr><tr><td class="specification-group" colspan="2"><div>Specifications</div></td></tr><tr><td>APPLICATION</td><td>SUITABLE FOR BRACKISH WATER</td></tr><tr><td>FILTER LIFE</td><td>APPROX 3000 LITRE / 6 MONTHS</td></tr><tr><td>FILTERS</td><td>SEDIMENT, PRECARBON, POST CARBON</td></tr><tr><td>FLOAT</td><td>MEMBRELLA</td></tr><tr><td>FR</td><td>MEMBRELLA /KFL</td></tr><tr><td>INLINE SET</td><td>MEMBRELLA</td></tr><tr><td>INPUT VOLTAGE</td><td>100-300 VOLT AC (50Hz)</td></tr><tr><td>INSTALLATION</td><td>COUNTER TOP</td></tr><tr><td>MAX.OPERATION TDS</td><td>4000 PPM</td></tr><tr><td>MEMBRANE TYPE</td><td>THIN FILM COMPOSITE</td></tr><tr><td>MIN.INLET PRESSURE / TEMP</td><td>0.3 kg / cm2, 10 °C</td></tr><tr><td>MODEL</td><td>WHALE 25</td></tr><tr><td>OPERATING VOLTAGE</td><td>48 VOLT (DC)</td></tr><tr><td>PRODUCT DIMENSION</td><td>21.1 (H) x 9.9 (W) x 16.7 (L)</td></tr><tr><td>PURIFICATION CAPACITY</td><td>25 LITRES PER HOUR</td></tr><tr><td>RECOVERY RATE</td><td>MORE THAN 30% AT 27°c ± 2°c</td></tr><tr><td>SMPS</td><td>MEMBRELLA / EQUALIANT</td></tr><tr><td>SOLENOID VALVE</td><td>MEMBRELLA / SLX</td></tr><tr><td>STORAGE CAPACITY</td><td>20 LITRES</td></tr><tr><td>TECHNOLOGY</td><td>REVERSE OSMOSIS SYSTEM</td></tr><tr><td>TOTAL POWER CONSUMPTION</td><td>50 W</td></tr><tr><td>TUBE 1/4</td><td>5 METERS</td></tr><tr><td>TUBE 3/8</td><td>2 METERS</td></tr><tr><td>WEIGHT</td><td>18 kg (Approx)</td></tr><tr><td>WARRENTY &amp;  SUPPORT</td><td>Since Whale  designs its purifiers and many of its parts  are a truly integrated system. Dealer only  can provide one-stop service ,guaranty and support for any service and maintenance, so most issues can be resolved in a single visit</td></tr></table>
      </div>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-09
        • 2020-05-03
        • 1970-01-01
        • 1970-01-01
        • 2020-03-25
        相关资源
        最近更新 更多