1 回答

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超4個(gè)贊
一些改進(jìn):
您的
soup
變量已被第二頁(yè)替換。您可以使用
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']
添加回答
舉報(bào)