【问题标题】:Unable to print description text in python无法在python中打印描述文本
【发布时间】:2021-03-04 08:41:08
【问题描述】:

因为我想从https://www.nike.com/gb/t/air-max-viva-shoe-ZQTSV8/DB5268-003 获取描述文本 但输出时不打印。

import requests
from bs4 import BeautifulSoup

import re
url ="https://www.nike.com/gb/t/air-max-viva-shoe-ZQTSV8/DB5268-003"

response = requests.get(url)

soup = BeautifulSoup(response.text, 'lxml')

headline1 = soup.find('h3', class_ = 'epdp-headline-md headline-1')

headline = soup.find('div',class_ = 'epdp-headline-sm body-1')
print(headline1,headline)

【问题讨论】:

  • 如果你看到[页面来源](view-source:nike.com/gb/t/air-max-viva-shoe-ZQTSV8/DB5268-003),没有epdp-headline-mdepdp-headline-sm的类,这就是你得到None的原因.
  • http https://www.nike.com/gb/t/air-max-viva-shoe-ZQTSV8/DB5268-003 | grep epdp-headline 没有给我任何输出,所以看起来你正在寻找的数据不在那里。它可能是动态内容,稍后通过 JavaScript 下载。
  • 那么我如何获取这些数据?
  • #brunns 有没有办法获取数据??

标签: python beautifulsoup python-requests


【解决方案1】:

这使用选择而不是查找。

import requests
from bs4 import BeautifulSoup

url ="https://www.nike.com/gb/t/air-max-viva-shoe-ZQTSV8/DB5268-003"

response = requests.get(url)

soup = BeautifulSoup(response.text, 'lxml')

description = soup.select('div.description-preview>p')
color = soup.select('div.description-preview>ul>li:nth-of-type(1)')
style = soup.select('div.description-preview>ul>li:nth-of-type(2)')

print(description[0].text)
print(color[0].text)
print(style[0].text)

【讨论】:

    【解决方案2】:

    试试这个

    # importing required libraries
    import requests
    from bs4 import BeautifulSoup
    
    # target URL to scrape
    url = "https://www.nike.com/gb/t/air-max-viva-shoe-ZQTSV8/DB5268-003"
    
    # headers
    headers = {
        'User-Agent': "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36"
        }
    
    # send request to download the data
    response = requests.request("GET", url, headers=headers)
    
    # parse the downloaded data
    data = BeautifulSoup(response.text, 'html.parser')
    

    你需要使用这个类pt6-sm prl6-sm prl0-lg来提取描述

    html_text = data.find('div',class_ = 'pt6-sm prl6-sm prl0-lg')
    

    返回描述。

    如果你想清理 HTML 对象,你可以使用下面的 sn-p

    import re
    
    def cleanhtml(raw_html):
      cleanr = re.compile('<.*?>')
      cleantext = re.sub(cleanr, '', raw_html)
      return cleantext
    
    txt = cleanhtml(str(html_text))
    
    print(txt)
    

    输出:

    Designed with every woman in mind, the mixed material upper of the Nike Air Max Viva
    features a plush collar, detailed patterning and intricate stitching. The new lacing
    system uses 2 separate laces constructed from heavy-duty tech chord, letting you find the
    perfect fit. Mixing comfort with style, it combines Nike Air with a lifted foam heel for
    and unbelievable ride that looks as good as it feels.Colour Shown: Black/Dark Citron/Green
    Abyss/Plum DustStyle: DB5268-003View Product Details
    

    【讨论】:

      猜你喜欢
      • 2014-04-18
      • 1970-01-01
      • 1970-01-01
      • 2013-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多