对于Python,我做错了什么,却没有看到BS4返回的<A>标记(链接)

2024-04-26 01:29:57 发布

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

我是python的新手,正在学习它。基本上,我试图从我的电子商务商店产品,是存储在下面的html的所有链接。我没有得到任何结果,但我似乎不知道为什么不。在

<h3 class="two-lines-name">
    <a title="APPLE IPOD IPOD A1199 2GB" target="_self" href="/Item/Details/APPLE-IPOD-IPOD-A1199-2GB/d1003297dbe7443c8953750f0c96c62a/400">
        APPLE IPOD IPOD A1199 2GB
    </a>
</h3>

这是我的python代码

^{pr2}$

没有数据的结果

Process finished with exit code 0

Tags: nameapple产品title链接htmlh3class
2条回答

你的表格错了。。。你在我的蜘蛛函数里调用我的蜘蛛。。。 删除最后一行的表格,它应该可以正常工作。在

如果在实际函数中有函数调用,那么实际上并不是在运行该函数,在更正错误(因为该错误不是传递给请求的有效url)之后,soup.findAll('a', {'h3 class': "two-lines-name"})将找不到任何内容:

def my_spider(max_pages):
    # use range from 1 to max pages 
    for i in range(1, max_pages+1):
        url = 'http://www.buya.com/Store/SAM-S-LOCKER/400?page={}'.format(i) # http:/?...
        source_code = requests.get(url)
        plain_text = source_code.content
        soup = BeautifulSoup(plain_text)
        # you want the h3 tags and to extract the href from the a tags
        for link in soup.findAll("h3", {'class': "two-lines-name"}):
            href = link.a["href"]
            print(href)


my_spider(5) # outside the function

输出:

^{pr2}$

相关问题 更多 >