无法通过Python中的网络抓取获取产品价格

时间:2019-01-30 16:56:36

标签: python loops web-scraping beautifulsoup request

我一直倾向于抓取网页,所以我决定进行一些练习。 我想使用此网站(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

2 个答案:

答案 0 :(得分:3)

您有很多选择

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部分中阅读更多内容。我将演示如何使用类和标记名过滤某些项目。

enter image description here

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

答案 1 :(得分:0)

<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