簡體   English   中英

BeautifulSoup Scraper 找不到文本?AttributeError: ResultSet object has no attribute 'find_all'

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

對編程非常陌生,對任何不良做法感到抱歉:

我試圖制作一個 web 刮刀,它確實會刮。com 用於我所在領域的工作列表,並在網上關注了一些關於它的文章,我以為我理解了,但現在我想我有一個誤解。

我正在嘗試抓取在 html 中找到的作業的位置,如下所示: html 代碼

為了抓取該位置,我被告知要執行以下操作:

 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 下加載它,而不是 span,所以我將代碼編輯如下:

 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方法時,它會返回。 您需要遍歷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!")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM