Python 3.x 我能做点什么让我的网页更好看吗?

Python 3.x 我能做点什么让我的网页更好看吗?,python-3.x,beautifulsoup,Python 3.x,Beautifulsoup,我得到了我想要的输出,但是到处都是,我如何使它更干净 import requests from bs4 import BeautifulSoup page = requests.get('https://www.titan.fitness/strength/dumbbells/rubber-coated-hex/rubber-hex-dumbbells-with-cast-iron-handle/HEXDBB-GROUP.html') soup = BeautifulSoup(page.tex

我得到了我想要的输出,但是到处都是,我如何使它更干净

import requests
from bs4 import BeautifulSoup

page = requests.get('https://www.titan.fitness/strength/dumbbells/rubber-coated-hex/rubber-hex-dumbbells-with-cast-iron-handle/HEXDBB-GROUP.html')
soup = BeautifulSoup(page.text, 'html.parser')

dumbbells_availability = soup.find(class_="set-items bundle-items container")
item = dumbbells_availability.find_all(class_="row product-detail set-item")
dumbbell1 = item[0]

availability = dumbbell1.find(class_="availability").get_text()
product_name = dumbbell1.find(class_="product-name").get_text()
print(availability)
print(product_name)
结果如下:

是的,你可以

只需使用
getText()
方法,将
strip
参数设置为
True

例如:

import requests
from bs4 import BeautifulSoup

page = requests.get('https://www.titan.fitness/strength/dumbbells/rubber-coated-hex/rubber-hex-dumbbells-with-cast-iron-handle/HEXDBB-GROUP.html')
soup = BeautifulSoup(page.text, 'html.parser')

dumbbells_availability = soup.find(class_="set-items bundle-items container")
item = dumbbells_availability.find_all(class_="row product-detail set-item")
dumbbell1 = item[0]

availability = dumbbell1.find(class_="availability").getText(strip=True)
product_name = dumbbell1.find(class_="product-name").getText(strip=True)
print(availability)
print(product_name)
输出:

availability:Out of Stock.Notify me when this is in stockSign up
5 LB Rubber Hex Dumbbells

定义“清洁剂”。这能回答你的问题吗?是否可以编写if-then语句?如果“缺货”写点什么?是的,有可能。例如,
print(“如果“缺货”就写点什么”。lower()在可用性中。lower()在其他“IDK O.O”)
PS.签出。