简体   繁体   English

beautifulsoupobject.findAll() 返回一个空列表

[英]beautifulsoupobject.findAll() returns an empty list

def ebay_scrape(urls):
    prices = []
    namets = []
    urlg = []
    for url in urls:
        try:
            res = requests.get(url)
            res.raise_for_status()
            soup = BeautifulSoup(res.text, 'html.parser')

        except (InvalidSchema, MissingSchema,):
            pass
        else: 
            try:
                price = soup.findAll (class_= "price").getText()
                prices.append(price)
                urlg.append(url)
            except (AttributeError):
                pass


    return urlg
urllist = (ebay_scrape(make_urls(names)))

I want to find extract all the prices from a query on an auction website.我想从拍卖网站上的查询中提取所有价格。 This script works fine when I do price = soup.find() but when I try soup.findAll it returns an empty list.当我执行 price = soup.find() 时,此脚本工作正常,但当我尝试 soup.findAll 时,它返回一个空列表。 I'm sure I'm just making some dumb mistake but any feedback would be appreciated!我确定我只是犯了一些愚蠢的错误,但我们将不胜感激任何反馈!

I looked at the HTML for one of that auction site's pages and there's no element with a class attribute of "price" exactly.我查看了该拍卖网站页面之一的 HTML,没有元素的 class 属性恰好是“价格”。

Also, you can't call .getText() on a findAll() result.此外,您不能在findAll()结果上调用.getText()

You can use a regular expression instead to find anything containing the word price:您可以使用正则表达式来查找包含单词价格的任何内容:

import re 

div_elements = soup.find_all('div',attrs={'class':re.compile('price')})

This turns up 252 results for one page.一页显示 252 个结果。
Then you can collect the prices by doing this:然后你可以通过这样做收集价格:

prices = []
for div_element in div_elements: 
    prices.append(div_element.text)

Result (which you'd have to clean up a bit):结果(你必须稍微清理一下):

In [191]: prices
Out[191]: 
['$49.34',
 '$92.00 | 46% off',
# ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM