BeautifulSoup Scraper can't find text?AttributeError: ResultSet object has no attribute 'find_all'

BeautifulSoup Scraper can't find text?AttributeError: ResultSet object has no attribute 'find_all'

超级编程新手,对于任何不好的做法深表歉意:

我试图制作一个网络抓取工具来抓取 indeed.com 我所在领域的工作列表,并且在网上关注了一些关于它的文章,我认为我理解了它,但现在我想我已经得到了一个误会。

我正在尝试抓取我在 html 中找到的工作位置,如下所示: html code

为了抓取该位置,我被告知要执行以下操作:

 grabbing location name
                c = div.find_all(name="span",attrs={"class":"location"})
                for span in c:
                    print(span.text)
                    job_post.append(span.text)

但是我注意到有时网页会在 div 下加载它,而不是跨度,所以我按如下方式编辑了代码:

 def find_location_for_job(self,div,job_post,city):
        div2 = div.find_all(name="div",attrs={"class":"sjcl"})
        print(div2)
        try:
            div3 = div2.find_all(name="div",attrs={"class":"location accessible-contrast-color-location"})
            job_post.append(div3.text)
        except:
            span = div2.find_all(name="span",attrs={"class":"location accessible-contrast-color-location"})
            job_post.append(span.text)

        print(job_post)

然而,有一半时间它仍然说它无法在 div/span 中找到文本,即使当我搜索帖子并看到它被标记为一个或另一个时也是如此。

AttributeError: ResultSet object has no attribute 'find_all'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?

请注意,我留下了我找到的代码,因为当使用 div 而不是 span 时它不会捕获结果。所以我的下一个故障排除步骤是将我的想法与他们的想法结合起来,如下所示:

def find_location_for_job(self,div,job_post,city):
    div2 = div.find_all(name="div",attrs={"class":"sjcl"})
    try:
        div3 = div2.find_all(name="div",attrs={"class":"location accessible-contrast-color-location"})
        for span in div3:
            job_post.append(span.text)
    except:
        div4 = div.findAll("span",attrs={"class":"location accessible-contrast-color-location"})
        for span in div4:
            job_post.append(span.text)

然而,此方法将整个位置列表放入它抓取的每个条目中(每个城市抓取 10 个发布,因此此方法将 10 个位置放入 10 个发布条目中的每一个)

谁能告诉我我哪里脑放屁了?

编辑:pastebin 中的完整代码:https://pastebin.com/0LLb9ZcU

div2 是一个 ResultSet,因为当您使用 BeautifulSoup 的 find_all 方法时,它就是 returns。您需要遍历 ResultSet 并像这样搜索内部字段:

def find_location_for_job(self, div, job_post, city): 
    div2 = div.find_all(name="div",attrs={"class":"sjcl"})
    for sjcl_div in div2:
        div3 = div2.find_all(name="div",attrs={"class":"location accessible-contrast-color-location"})
        div4 = div.find_all("span",attrs={"class":"location accessible-contrast-color-location"})
        if div3:
            for span in div3:
                job_post.append(span.text)
        elif div4:
            for span in div4:
                job_post.append(span.text)
        else:
            print("Uh-oh, couldn't find the tags!")