使用Beautiful Soup选择HTML页面值

时间:2018-06-10 09:30:49

标签: python python-3.x beautifulsoup

我一直试图想出这个问题几个小时,而且我正在努力。我尝试的每一种方法都没有提供正确的价值。

import requests
from bs4 import BeautifulSoup

r = requests.get('https://www.off---white.com/en/GB/products/omia065s188000160100')
soup = BeautifulSoup(r.content, 'html.parser')

我想从网页(https://www.off---white.com/en/GB/products/omia065s188000160100

中提取以下值
Name = LOW 3.0 SNEAKER
Price = £ 415
img_url = https://cdn.off---white.com/images/156365/large_OMIA065S188000160100_4.jpg?1498202305

如何使用Beautiful Soup提取这3个值?

1 个答案:

答案 0 :(得分:1)

import requests
from bs4 import BeautifulSoup

# Get prod name
r = requests.get('https://www.off---white.com/en/GB/products/omia065s188000160100')
soup = BeautifulSoup(r.text, 'html.parser')
spans = soup.find_all('span', {'class' : 'prod-title'})
data = [span.get_text() for span in spans]
prod_name = ''.join(data)

# Find prod price
spans = soup.find_all('div', {'class' : 'price'})
data = [span.get_text() for span in spans]
prod_price = ''.join(data)

# Find prod img
spans = soup.find_all('img', {'id' : 'image-0'})

for meta in spans:
    prod_img = meta.attrs['src']

print(prod_name)
print(prod_price)
print(prod_img)