繁体   English   中英

Web 抓取新闻文章和关键字搜索

[英]Web scraping news articles and keyword search

我有一个代码可以获取网页中新闻文章的标题。 我使用了一个 for 循环,在其中我获得了 4 个新闻网站的标题。 我还实现了一个单词搜索,它告诉我们使用“冠状病毒”这个词的文章的数量。 我想要单词搜索,它可以告诉我每个网站中带有“冠状病毒”一词的文章数量。 现在,我得到的 output 是所有网站中使用“冠状病毒”这个词的次数。 请帮助我,我必须尽快提交这个项目。 以下是代码:

from bs4 import BeautifulSoup
from bs4.dammit import EncodingDetector
from newspaper import Article
import requests
URL=["https://www.timesnownews.com/coronavirus","https://www.indiatoday.in/coronavirus", "https://www.ndtv.com/coronavirus?pfrom=home-mainnavigation"]
for url in URL:
    parser = 'html.parser'  
    resp = requests.get(url)
    http_encoding = resp.encoding if 'charset' in resp.headers.get('content-type', '').lower() else None
    html_encoding = EncodingDetector.find_declared_encoding(resp.content, is_html=True)
    encoding = html_encoding or http_encoding
    soup = BeautifulSoup(resp.content, parser, from_encoding=encoding)
    
    links = []
    for link in soup.find_all('a', href=True):
        if "javascript" in link["href"]:
            continue
        links.append(link['href'])
            
    count = 0
     
            
    for link in links:
        try:
            article = Article(link)
            article.download()
            article.parse()
            print(article.title)
            if "COVID" in article.title or "coronavirus" in article.title or "Coronavirus"in article.title or "Covid-19" in article.title or "COVID-19" in article.title :
                    count += 1
    
        except:
            pass
         
        
print(" number of articles with the word COVID:")
print(count)

实际上,您只获得最后一个站点计数。 如果你想得到那么所有的,append 它到一个列表中,然后你可以打印每个站点的计数。

首先创建一个空列表和 append 每次迭代的最终计数:

URL = ["https://www.timesnownews.com/coronavirus", "https://www.indiatoday.in/coronavirus",
       "https://www.ndtv.com/coronavirus?pfrom=home-mainnavigation"]
Url_count = []

for url in URL:
    parser = 'html.parser'
    ...
    ...
        except:
            pass

    Url_count.append(count)

然后可以使用zip打印结果:

for url, count in zip(URL, Url_count):
    print("Site:", url, "Count:", count)

暂无
暂无

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

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