python - 我正在抓取沃尔玛,但是每当我输入带有要抓取的搜索 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,它会显示价格。我面临的问题是当我运行它时,我的输出是“无”。
解决方案
变量文本永远不会被创建,因为在循环之外
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")
推荐阅读
- php - array_search 没有找到第一个数组元素
- angular - 从类调用方法
- python-3.x - 在 Python 的 json 文件中保存多个 api 调用
- python - 散点图matplotlib:处理熊猫日期时间作为索引
- mysql - 如何修复低版本 SQL 上的 Magento 时间戳问题
- nativescript - NativeScript Sidekick 无法启动
- ios - 批量 NSLayoutConstraint 激活/停用
- ruby - 坚持这个 Ruby 逻辑
- asp.net-mvc - 当我试图到达最近的地方时,我在运行时遇到了这个错误
- c# - 如何根据提交按钮在foreach循环内显示/隐藏行