网站抓取时需要一个位置

时间:2019-03-29 18:33:33

标签: python-3.x beautifulsoup

我在弄清楚为什么我无法从Lowes网站上获取价格时遇到了一些麻烦。下面的评论文字是试图抓住我已经尝试过的价格。我当前使用的价格采集器会打印“输入您的位置”,这使我相信我必须向网站提供我的位置才能获得价格。您将采取什么措施来解决这个问题?

import requests
from bs4 import BeautifulSoup

url = 'https://www.lowes.com/search?searchTerm=2x4'

r = requests.get(url)

html = r.text

soup = BeautifulSoup(html, 'lxml')

product_container = soup.find_all('div',{'class':'product-wrapper-right'})

i = 0

for product in product_container:
    soup2 = BeautifulSoup(str(product), 'lxml')

    productName = product.div.a.p.text
    print(productName)

    priceContainer = soup2.find("div", {"class":"product-pricing"})
    productPrice = priceContainer.div.text
    print(productPrice)

    productURL = product.div.a.get('href')
    print("lowes.com" + productURL)
    i = i+1


    #productPrice = product.div.div.span.text
    #print(productPrice)

    #soup3 = soup2.find('div', {"class":"product-pricing"})
    #productPrice = soup3.div.span.text
    #print(productPrice)

    #step1 = product.find_all('div')[1]
    #step2 = step1.find_all('div')[1]
    #productPrice = step2.span.text
    #print(productPrice)

所有评论文本也未能在不同程度上抓住价格。我希望它们都能打印出价格,但它们都将返回括号,即“输入您的位置”,“无”。

0 个答案:

没有答案
相关问题