BeautifulSoup findAll未返回结果

ldfqzlk8  于 2022-10-22  发布在  Python
关注(0)|答案(1)|浏览(180)

我想知道这个页面的产品名称和价格。我几乎重复了同样的事情,我为产品名称和价格做了同样的事,但我什么都没有得到。

from urllib.request import Request, urlopen
from bs4 import BeautifulSoup as bSoup

header = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:77.0) Gecko/20100101 Firefox/77.0'}
url = "https://www.walmart.ca/search?q=lettuce"
req = Request (url = url, headers = header)

client = urlopen (req)
pageHtml = client.read()
client.close()

pageSoup = bSoup(pageHtml, 'html.parser')

products = pageSoup.findAll ("div", {"class":"css-155zfob e175iya63"})
print (len(products)) #prints 15, like expected
for product  in products:
    pass

prices = pageSoup.findAll ("div", {"class":"css-8frhg8 e175iya65"})
print (len(prices)) #prints 0 and idk why :/
for price in prices:
    pass
vlju58qv

vlju58qv1#

页面https://www.walmart.ca/search?q=lettuce没有返回您期望的内容:

curl -s -H 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:77.0) Gecko/20100101 Firefox/77.0' 'https://www.walmart.ca/search?q=lettuce' | grep 'css-8frhg8'

您可能在浏览器中看到了该类,其中内容在运行时通过JavaScript部分呈现。这意味着您需要使用一个能够模拟具有JavaScript支持的浏览器的库。

相关问题