简体   繁体   English

使用Beautiful Soup选择HTML页面值

[英]Selecting HTML page values using Beautiful Soup

I have been trying to figure this out for a few hour and it is doing my head in. Every method I try is not presenting the correct value. 我已经尝试了好几个小时,并且正在解决这个问题。我尝试的每种方法均未提供正确的值。

import requests
from bs4 import BeautifulSoup

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

I want to extract the following values from the webpage ( https://www.off---white.com/en/GB/products/omia065s188000160100 ) 我要从网页中提取以下值( 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

How would I extract these 3 values using Beautiful Soup? 如何使用Beautiful Soup提取这3个值?

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM