第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何從html中提取價(jià)格并存儲(chǔ)到變量中?

如何從html中提取價(jià)格并存儲(chǔ)到變量中?

守著一只汪 2023-03-16 17:56:05
我試圖從這些褲子中提取價(jià)格并將其放入變量中。我看到其他解決方案在 html 中有類(lèi)似“itemprop”的內(nèi)容,但我的沒(méi)有。這是一個(gè)屏幕截圖:這是我到目前為止的代碼:import requestsimport timefrom bs4 import BeautifulSoupimport jsonurl = 'https://www.urbanoutfitters.com/search?q=cargo%20pant&sayt=true'headers = {'User-Agent':'Mozilla/5.0'}response = requests.get(url, headers = headers)response.status_codeprint (response)soup = BeautifulSoup(response.content,'html.parser')page = requests.get("https://www.patagonia.ca/shop/mens-hard-shell-jackets-vests")soup = BeautifulSoup(page.content, 'html.parser')div_price = []# Loop on elementsfor pant in soup.find_all('p', {'class':'c-pwa-product-price c-pwa-product-tile__price'}):    span_price = pant.find('span', {'aria-label': 'class'})    if span_price:        div_price.append(span_price.get('content'))print(div_price)謝謝?。。?!
查看完整描述

1 回答

?
絕地?zé)o雙

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超4個(gè)贊

一些改進(jìn):

  1. 您的soup變量已被第二頁(yè)替換。

  2. 您可以使用soup.find_all('span', {'class':'c-pwa-product-price__current'})直接搜索價(jià)格。

像下面這段代碼:

import requests

import time

from bs4 import BeautifulSoup

import json


url = 'https://www.urbanoutfitters.com/search?q=cargo%20pant&sayt=true'

headers = {'User-Agent':'Mozilla/5.0'}

response = requests.get(url, headers = headers)

soup = BeautifulSoup(response.content,'html.parser')



# page = requests.get("https://www.patagonia.ca/shop/mens-hard-shell-jackets-vests")

# print(page.text)

# soup = BeautifulSoup(page.content, 'html.parser')


div_price = []

# Loop on elements

for pant in soup.find_all('span', {'class':'c-pwa-product-price__current'}):

    div_price.append(pant.text)

print(div_price)

結(jié)果:


['$59.00', '$149.00', '$69.00', '$69.00', '$69.00', '$79.00', '$69.00', '$69.00', '$184.00', '$59.00', '$54.00', '$54.00', '$69.00', '$99.00', '$85.00', '$59.00', '$69.00', '$49.00', '$74.00', '$64.00', '$160.00', '$180.00', '$55.00', '$85.00', '$125.00', '$79.00', '$59.00', '$110.00', '$69.00', '$19.99', '$189.99', '$19.99', '$39.99', '$69.00', '$49.00', '$59.00', '$59.00', '$69.00', '$49.00', '$29.99', '$79.00', '$79.00', '$79.00', '$95.00', '$79.00', '$79.00', '$74.00', '$69.00', '$79.00', '$69.00', '$64.00', '$74.00', '$190.00', '$64.00', '$29.99', '$96.00', '$320.00', '$96.00', '$49.99', '$123.99', '$69.99', '$19.99', '$39.99', '$19.99', '$19.99', '$29.99', '$59.99', '$29.99', '$39.99', '$19.99', '$74.99', '$29.99', '$129.99']



查看完整回答
反對(duì) 回復(fù) 2023-03-16
  • 1 回答
  • 0 關(guān)注
  • 131 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)