只能解析websi的第一页

2024-04-27 03:11:34 发布

您现在位置:Python中文网/ 问答频道 /正文

x = 0
htmlpar= ""
dictlist = []
datalist = [223, 236, 250, 263, 277, 290, 304, 317, 331, 344, 358, 371, 385, 398, 412, 425, 439, 452, 466, 479]
from urllib.request import urlopen
from html.parser import HTMLParser
html = urlopen("http://steamcommunity.com/market/search?q=&category_730_ItemSet%5B%5D=any&category_730_Weapon%5B%5D=any&appid=730#p5_quantity_desc").read().decode('utf-8')
class MyHTMLParser(HTMLParser):
    def handle_data(self, data):
        global x
        global datalist
        global dictlist
        x += 1
        if x in datalist:
            dictlist.append(data)
MyHTMLParser().feed(html)
print(dictlist)
input()

输出:

['0.03', 'Sticker Capsule',  
 '0.03', 'Sticker Capsule 2', 
 '0.04', 'eSports Winter Case',  
 '0.04', 'CS:GO Weapon Case 3', 
 '0.11', 'Winter Offensive Weapon Case', 
 '0.04', 'Community Sticker Capsule 1', 
 '0.10', 'CS:GO Weapon Case 2', 
 '0.39', 'Operation Phoenix Weapon Case', 
 '1.10', 'Huntsman Weapon Case', 
 '0.72', 'eSports Case']

当我尝试解析任何一个页面时,它只会继续执行steam市场上的第一个页面:http://steamcommunity.com/market/search?q=&category_730_ItemSet%5B%5D=any&category_730_Weapon%5B%5D=any&appid=730#p5_quantity_desc

这个url显然有“#p5”(第5页),但是第5页最上面的是一个5-7,而它一直在打印标签胶囊(第一页的顶部)。你知道吗

我觉得这可能只适用于steam,因为在网站上,如果你更改页面,它只会重新加载框中的数据,而不是整个网页,但我想确保我的代码中没有愚蠢的东西。你知道吗


Tags: fromimportdatahtmlany页面globalcase