抓取时发生AttributeError [英] AttributeError while scraping

查看:54
本文介绍了抓取时发生AttributeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试抓取网站,但出现此错误:

I am trying to scrape a website, I have got this error:

AttributeError:'NoneType'对象没有属性'text'

AttributeError: 'NoneType' object has no attribute 'text'

at

---> 12             for x in soup.select("div.site-content")]

使用的代码是:

rq = req.get("https://stopcensura.net/category/cronaca")
soup = BeautifulSoup(rq.content, 'html.parser')
scrape_info = [(x.h3.a.text, x.time.text)
            for x in soup.select("div.site-content")]

我想了解标题( entry-title ),日期( class ="date" ),作者(< divclass ="by-author vcard author" ...</div> )和内容( div class ="entry-content" ).我认为问题可能出在选择正确的标签上,但是我不确定.

I would like to get infnormation on title (entry-title), date (class="date"), the author (<div class="by-author vcard author">... </div>),and the content (div class="entry-content"). I think the problem may be in selecting the right tags, but I am not sure of this.

任何帮助和建议将不胜感激.

Any help and suggestions will be appreciated.

推荐答案

尝试稍微修改CSS选择器以获得正确的输出:

Try modifying the CSS Selectors a bit to get the correct output:

import requests
from bs4 import BeautifulSoup


URL = "https://stopcensura.net/category/cronaca"

soup = BeautifulSoup(requests.get(URL).content, "html.parser")

for tag in soup.select(".entry-header"):
    print(tag.select_one(".entry-title a").text)
    print(tag.select_one(".date a").text)
    print(tag.select_one(".by-author a").text)
    print(tag.find_next(class_="entry-content").text.strip())
    print("-" * 50)

输出:

Manifestanti inscenano corteo funebre a Napoli: "Conte hai ucciso l’Italia" (VIDEO)
2 Nov 2020
Redazione
"L’Italia che lotta non si ferma: decimo giorno di protesta. Numerosi cittadini a Napoli protestano contro…
--------------------------------------------------
Foggia, immigrato irrompe in casa armato: sprangate ad una famiglia italiana
2 Nov 2020
Redazione
FOGGIA – La serenità di una famiglia foggiana è stata interrotta da un immigrato 21enne, irregolare…
--------------------------------------------------
Decine di agenti per chiudergli locale. Titolare: "Sono un lavoratore, non un camorrista"
1 Nov 2020
Redazione
"Dopo sei giorni di eroica disobbedienza civile il ristorante Bistrot Bonaccorsi a Bologna viene chiuso con…
--------------------------------------------------
Roma, popolo in marcia verso il Parlamento: agenti caricano i manifestanti (VIDEO)
31 Ott 2020
Redazione
"L’Italia non si ferma: ottavo giorno di proteste. Roma, Campo dei Fiori, cittadini in marcia pacifica…
--------------------------------------------------
Strage di Nizza, leader islamico: "Se i fedeli si arrabbiano, poi succedono tante cose" (VIDEO)
31 Ott 2020
Redazione
Le parole del leader della comunità musulmana di Roma, Ahah Mohammed Taifur Rahman, che ieri durante…
--------------------------------------------------
Proteste a Firenze: "Non siamo fascisti, abbiamo votato per quei pezzi di mer** del Pd" (VIDEO)
31 Ott 2020
Redazione
"Proteste a Firenze contro lockdown e Governo Conte. Manifestanti tuonano contro l’informazione di regime: "Non siamo…
--------------------------------------------------

这篇关于抓取时发生AttributeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆