【问题标题】:Can't get prices of a product by web scraping in Python [closed]无法通过 Python 中的网络抓取来获取产品价格 [关闭]
【发布时间】:2019-01-30 16:56:36
【问题描述】:

我一直倾向于网络爬虫,所以我决定练习一下。 使用这个网站(https://lista.mercadolivre.com.br/razer?matt_tool=6263806&matt_word=RAZER_MERCADO_LIVRE&gclid=CjwKCAiAs8XiBRAGEiwAFyQ-ejETB12X8G75icDJLMkW4ChSyBsJLrL3wZv_o3oZb8zvtUsc5D1tZBoCsNEQAvD_BwE),我想我会尝试在第一页收集每个产品的描述和价格。我能够得到很好的描述,但我遇到了价格问题。

例如,给定第一个产品,价格是数字 559。但是,当我使用 produto1.div.span.text 时,Python 只给了我“R$”,这是我不想要的。 我该怎么做才能得到实际价格?

我的代码:

url = 'https://lista.mercadolivre.com.br/razer?matt_tool=6263806&matt_word=RAZER_MERCADO_LIVRE&gclid=CjwKCAiAs8XiBRAGEiwAFyQ-ejETB12X8G75icDJLMkW4ChSyBsJLrL3wZv_o3oZb8zvtUsc5D1tZBoCsNEQAvD_BwE'
response = get(url)
html_soup = BeautifulSoup(response.text, 'html.parser')
produtos = html_soup.find_all('div', class_ = "item__info item__info--with-reviews")

produto1 = produtos[0]
produto1.div.span.text

【问题讨论】:

  • 欢迎来到 StackOverflow。请按照您创建此帐户时的建议阅读并遵循帮助文档中的发布指南。 Minimal, complete, verifiable example 适用于此。在您发布 MCVE 代码并准确描述问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中并重现您描述的问题。您的代码未按照发布的方式运行:BS 未导入,get 未解析。

标签: python loops web-scraping beautifulsoup request


【解决方案1】:

你有很多选择

a) 如果您只需要价格,那么您只需定位价格即可。

produtos = html_soup.find_all('span', class_ = "price__fraction")
print([item.text for item in produtos])

输出:

['559', '395', '378', '66', '349', '148', '39', '422', '39', '195', '314', '63', '844', '147', '399', '899', '239', '739', '469', '564', '28', '487', '1.189', '169', '324', '32', '899', '399', '168', '234', '274', '168', '624', '854', '29', '156', '189', '209', '267', '595', '273', '189', '299', '289', '249', '686', '1.489', '449']

b) 如果您只想获得价格,但想选择div 以便以后获得更多商品,那也可以。

produtos = html_soup.find_all('div', class_ = "item__info item__info--with-reviews")
produto1 = produtos[0]
print(produto1.find('span',class_='price__fraction').text)

输出:

559

选择产品 div 后,从中获取数据只是使用 BeautifulSoup 进行正确选择的问题。您可以在文档的Searching the tree 部分阅读更多内容。我将演示如何使用类和标签名称过滤一些项目。

from requests import get
from bs4 import BeautifulSoup
url = 'https://lista.mercadolivre.com.br/razer?matt_tool=6263806&matt_word=RAZER_MERCADO_LIVRE&gclid=CjwKCAiAs8XiBRAGEiwAFyQ-ejETB12X8G75icDJLMkW4ChSyBsJLrL3wZv_o3oZb8zvtUsc5D1tZBoCsNEQAvD_BwE'
response = get(url)
html_soup = BeautifulSoup(response.text, 'html.parser')
produtos = html_soup.find_all('div', class_ = "item__info item__info--with-reviews")
produto1 = produtos[0]
#price symbol
print(produto1.find('span',class_='price__symbol').text)
#price fraction / price
print(produto1.find('span',class_='price__fraction').text)
#main title
print(produto1.find('span','main-title').text.strip())

输出:

R$
559
Razer Combo Holiday - Cynosa+goliathus+deathadder+kraken

【讨论】:

  • 这真的很有帮助!太想你了!我会更好地审视我的选择,尝试多学一点,看看什么效果更好。祝您有美好的一天!
【解决方案2】:

<div> 对象中有 10 个 span 标签。你只是抓住了第一个<span> 标签。

您可以通过以下方式获取下一个标签:

produto1.div.span.find_next('span').text

你可以通过找到所有<span>标签来查看它,然后循环遍历它:

import requests
import bs4

url = 'https://lista.mercadolivre.com.br/razer?matt_tool=6263806&matt_word=RAZER_MERCADO_LIVRE&gclid=CjwKCAiAs8XiBRAGEiwAFyQ-ejETB12X8G75icDJLMkW4ChSyBsJLrL3wZv_o3oZb8zvtUsc5D1tZBoCsNEQAvD_BwE'
response = requests.get(url)
html_soup = bs4.BeautifulSoup(response.text, 'html.parser')
produtos = html_soup.find_all('div', class_ = "item__info item__info--with-reviews")

produto1 = produtos[0]

span_tags = produto1.find_all('span')

i = 0
for span in span_tags:
    print ('Element: '+ str(i) +' Text: ' + span.text.strip())
    i += 1

输出:

Element: 0 Text: R$
Element: 1 Text: 559
Element: 2 Text: 12x   R$ 53 43
Element: 3 Text: 12x
Element: 4 Text: R$ 53 43
Element: 5 Text: 
Element: 6 Text: Razer Combo Holiday - Cynosa+goliathus+deathadder+kraken
Element: 7 Text: por Razer
Element: 8 Text: por Razer
Element: 9 Text: por Razer

【讨论】:

  • 很好的答案!您刚刚为我提供了浏览代码的新方法,这真的很有帮助!也有美好的一天!
猜你喜欢
  • 1970-01-01
  • 2023-03-16
  • 1970-01-01
  • 2013-02-23
  • 2014-05-04
  • 1970-01-01
  • 1970-01-01
  • 2015-11-03
  • 1970-01-01
相关资源
最近更新 更多