无法在python中打印说明文本

2024-04-29 03:20:43 发布

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

因为我想从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)

Tags: httpsimportcomwwwairrequestsmaxsoup
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对象,可以使用下面的代码段

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

这将使用选择而不是查找

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)

相关问题 更多 >