【问题标题】:newspaper(python) get all cnn news url报纸(python)获取所有cnn新闻网址
【发布时间】:2019-01-09 16:39:45
【问题描述】:

例如在这个 url (https://edition.cnn.com/search/?q=%20news&size=10&from=5540&page=555)

在 html 文件中我可以找到这个链接(html 标签)

<div class="cnn-search__result-thumbnail">         
     <a href="https://www.cnn.com/2018/03/27/asia/north-korea-kim-jong-un-china-visit/index.html">
  <img src="./Search CNN - Videos, Pictures, and News - 
    CNN.com_files/180328104116china-xi-kim-story-body.jpg">
 </a>

但在这段代码中

    cnn_paper = newspaper.build(url, memoize_articles=False)
     for article in cnn_paper.articles:
          print(article.url) 

我找不到新闻链接

https://edition.cnn.com/search/?q=%20news&size=10&from=5540&page=555 https://edition.cnn.com/search/?q=%20news&size=10&from=5550&page=556

获取相同的链接

【问题讨论】:

  • 请详细说明您的问题。你到底在问什么?
  • 我想获取特定站点中的所有新闻链接
  • 您能否将您发送的网址的值作为参数发布在报纸库的 .build() 方法中。

标签: python html python-newspaper


【解决方案1】:

这是你想要的吗?

from bs4 import BeautifulSoup
import urllib.request

for numb in ('1', '100'):
    resp = urllib.request.urlopen("https://edition.cnn.com/search/?q=%20news&size=10&from=5540&page=555")
    soup = BeautifulSoup(resp, from_encoding=resp.info().get_param('charset'))

    for link in soup.find_all('a', href=True):
        print(link['href'])

或者,也许是这个?

from bs4 import BeautifulSoup
from bs4.dammit import EncodingDetector
import requests

resp = requests.get("https://edition.cnn.com/search/?q=%20news&size=10&from=5540&page=555")
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, from_encoding=encoding)

for link in soup.find_all('a', href=True):
    print(link)

【讨论】:

  • 这样我可以链接除了新闻链接新闻在url中包含新闻日期
【解决方案2】:

搜索结果从来自不同请求的 JSON 文件动态显示: https://search.api.cnn.io/content?q=news&size=50&from=0

大小最大为 50。

res = requests.get("https://search.api.cnn.io/content?q=news&size=50&from=0")
links = [x['url'] for x in res.json()['result']]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-31
    • 1970-01-01
    • 2020-12-23
    • 1970-01-01
    • 2019-07-01
    • 2017-11-11
    • 2016-10-10
    • 2018-11-15
    相关资源
    最近更新 更多