首页 > 解决方案 > 我正在抓取沃尔玛,但是每当我输入带有要抓取的搜索 URL 的函数的参数时,当我尝试打印它时,我什么也得不到

问题描述

import ssl
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup


context = ssl._create_unverified_context()


def PriceOfLegos(Site):
    price = []
    title = []
    LegoWebsite = Site
    uLegoWebsite = uReq(LegoWebsite, context=context)
    LegoWebsiteHTML = uLegoWebsite.read()
    uLegoWebsite.close()
    LegoWebsiteSoup = soup(LegoWebsiteHTML, "html.parser")
    for x in LegoWebsiteSoup.find_all("span", {"class": "visuallyhidden"}):
        text = x.get_text()
    if text[0] == "$":
        price.append(text[1:])
    for x in LegoWebsiteSoup.find_all("a", {"class": "product-title-link line-clamp line-clamp-2"}):
        title_text = x.get_text()
        title.append(title_text)
    for x in price:
        print("$", x, sep="")


z = PriceOfLegos("https://www.walmart.com/search/?query=Lego%20horse")
print(z)

当代码不是函数并且 LegoWebsite = URL 时,抓取工作。唯一的问题是我希望它更加动态,所以我可以在沃尔玛上输入任何搜索 URL,它会显示价格。我面临的问题是当我运行它时,我的输出是“无”。

标签: pythonpython-3.xfunctionweb-scraping

解决方案


变量文本永远不会被创建,因为在循环之外

if text[0] == "$":
    price.append(text[1:])

当您打印出您的价目表时是空的,因为没有附加任何内容,因为:

if text[0] == "$":

永远不会得到 True cos 文本变量对于 if 语句不存在

尝试这个:

import ssl
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup


context = ssl._create_unverified_context()


def PriceOfLegos(Site):
    price = []
    title = []
    LegoWebsite = Site
    uLegoWebsite = uReq(LegoWebsite, context=context)
    LegoWebsiteHTML = uLegoWebsite.read()
    uLegoWebsite.close()
    LegoWebsiteSoup = soup(LegoWebsiteHTML, "html.parser")
    for x in LegoWebsiteSoup.find_all("span", {"class": "visuallyhidden"}):
        text = x.get_text()
        if text[0] == "$":
            price.append(text[1:])
    for x in LegoWebsiteSoup.find_all("a", {"class": "product-title-link line-clamp line-clamp-2"}):
        title_text = x.get_text()
        title.append(title_text)
    for x in price:
        print("$", x, sep="")


PriceOfLegos("https://www.walmart.com/search/?query=Lego%20horse")

推荐阅读