为了账号安全,请及时绑定邮箱和手机立即绑定

如何从html中提取价格并存储到变量中?

如何从html中提取价格并存储到变量中?

守着一只汪 2023-03-16 17:56:05
我试图从这些裤子中提取价格并将其放入变量中。我看到其他解决方案在 html 中有类似“itemprop”的内容,但我的没有。这是一个屏幕截图:这是我到目前为止的代码: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 回答

?
绝地无双

TA贡献1946条经验 获得超4个赞

一些改进:

  1. 您的soup变量已被第二页替换。

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

像下面这段代码:

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)

结果:


['$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']



查看完整回答
反对 回复 2023-03-16
  • 1 回答
  • 0 关注
  • 77 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信