首页 > 解决方案 > Python:网站的类打印出一个空列表

问题描述

我正在尝试使用以下脚本抓取类stats(item price和) 中的所有内容:price changes

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

url = "https://secure.runescape.com/m=itemdb_oldschool/Dragon+warhammer/viewitem?obj=13576" 

uClient = uReq(url)
page_html = uClient.read()

page_soup = soup(page_html, "html.parser")

price = page_soup.find_all(class_ = "stats")

print(price)

我得到这个打印:

[]

我将这个脚本用于我所有的其他 webscrappes,这是我第一次得到这样的东西。

我试着环顾四周,问了一些人,我仍然找不到解决方案。

标签: pythonweb-scrapingbeautifulsoup

解决方案


检查page_soup变量的值:

<html style="height:100%"><head><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"><meta name="format-detection" content="telephone=no"><meta name="viewport" content="initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><script type="text/javascript" src="/_Incapsula_Resource?SWJIYLWA=719d34d31c8e3a6e6fffd425f7e032f3"></script><script src="/Criciousand-meth-shake-Exit-be-till-in-ches-Shad" async></script></head><body style="margin:0px;height:100%"><iframe id="main-iframe" src="/_Incapsula_Resource?SWUDNSAI=30&xinfo=7-5532445-0%20NNNY%20RT%281620414344651%2056%29%20q%280%20-1%20-1%201%29%20r%281%20-1%29%20B12%2814%2c0%2c0%29%20U5&incident_id=1233000410021120939-28775082668132935&edet=12&cinfo=0e000000d694&rpinfo=0&cts=UC3pkO3NyZP9f4EA4%2fm56lwz1Y6BhOV6CwF4xNVSeeeNp96DzLjUUDt3%2b5RYEDst" frameborder=0 width="100%" height="100%" marginheight="0px" marginwidth="0px">Request unsuccessful. Incapsula incident ID: 1233000410021120939-28775082668132935</iframe></body></html>

如果您以隐身模式访问该网站,您将看到相同的结果。

由于该页面没有名为 'stats' 的类,因此page_soup.find_all(class_ = "stats")的结果是一个空列表。


推荐阅读