"Load more" 按钮内的 href 粘贴时不会带来更多文章 URL

href inside "Load more" button doesn't bring more articles when pasting URL

我正在尝试抓取此网站:

https://noticias.caracoltv.com/colombia

最后你可以找到一个“Cargar Más”按钮,它带来了更多新闻。到目前为止,一切都很好。但是,当检查该元素时,它说它加载了一个 link,如下所示:https://noticias.caracoltv.com/colombia?00000172-8578-d277-a9f3-f77bc3df0000-page=2,如下所示:

问题是,如果我在浏览器中输入这个,我会得到与调用原始网站时相同的消息。因此,我看到我能够抓取网站的唯一方法是创建一个递归点击的脚本。问题是我需要2019年之前的新闻,所以看起来不太可行。

另外,在检查事件侦听器时我看到了这个:

但我不确定如何利用它来发挥我的优势。

我错过了什么吗?有什么方法可以通过 link 访问旧新闻(或者 API 会更好,但我没有找到对 API 的任何调用)。

我目前正在使用 Python 进行抓取,但我正处于调查阶段,因此没有任何代码可以证明这是有意义的。 非常感谢!

请检查 Query String format @ wiki

您错过了一个 & 标记

不幸的是,似乎没有很好的 api 或其他方式来批量获取数据。您将需要遍历“页面”/“加载更多”,然后解析 html.

需要一些时间,但此代码会为您完成。

import requests 
from bs4 import BeautifulSoup
import re
import json
from dateutil import parser
import datetime
import pandas as pd

headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'}

rows = []
endYear = 2019
continueLoop = True
page = 1
while continueLoop == True:

    url = f'https://noticias.caracoltv.com/colombia?00000172-8578-d277-a9f3-f77bc3df0000-page={page}'
    response = requests.get(url, headers=headers)
    
    soup = BeautifulSoup(response.text, 'html.parser')
    
    if page == 1:
        mainArticle = soup.find('div', {'class':'ListU'})
        jsonStr = str(mainArticle.find('script'))
        jsonStr = re.search('({.*})', jsonStr).group(1)
        jsonData = json.loads(jsonStr)
        
        articlePublished = jsonData['datePublished']
        dt = parser.parse(articlePublished)
        
        print(f"{dt.day}-{dt.month}-{dt.year} : {jsonData['headline']}")
        rows.append(jsonData)
        
        subArticles = soup.find_all('li', {'class':'ListE-items-item'})
        for subArticle in subArticles:
            jsonStr = str(subArticle.find('script'))
            jsonStr = re.search('({.*})', jsonStr).group(1)
            jsonData = json.loads(jsonStr)
            
            articlePublished = jsonData['datePublished']
            dt = parser.parse(articlePublished)
            
            print(f"{dt.day}-{dt.month}-{dt.year} : {jsonData['headline']}")
            rows.append(jsonData)

    #get bottom articles
    loadMore = soup.find('ps-list-loadmore', {'class':'ListD'})
    articles = loadMore.find_all('li',{'class':'ListD-items-item'})
    for article in articles:
        if continueLoop == True:
            try:
                jsonStr = str(article.find('script'))
                jsonStr = re.search('({.*})', jsonStr).group(1)
                jsonData = json.loads(jsonStr)
                
                articlePublished = jsonData['datePublished']
                dt = parser.parse(articlePublished)
            except:
                headline = article.find('a', href=True)['title']
                articlePublished = article.find('div', {'PromoB-timestamp'})['data-timestamp']
                url = article.find('a', href=True)['href']
                
                jsonData = {
                    'headline':headline,
                    'datePublished':articlePublished,
                    'url':url}
            
            
            if endYear == dt.year:
                print('Done')
                continueLoop = False
            
            else:
                print(f"{dt.day}-{dt.month}-{dt.year} : {jsonData['headline']}")
                rows.append(jsonData)
            
    page += 1


df = pd.DataFrame(rows)

输出:

print(df)
               @context  ...                                              video
0     http://schema.org  ...  {'@context': 'http://schema.org', '@type': 'Vi...
1     http://schema.org  ...  {'@context': 'http://schema.org', '@type': 'Vi...
2     http://schema.org  ...  {'@context': 'http://schema.org', '@type': 'Vi...
3     http://schema.org  ...                                                NaN
4     http://schema.org  ...  {'@context': 'http://schema.org', '@type': 'Vi...
                ...  ...                                                ...
6864  http://schema.org  ...  {'@context': 'http://schema.org', '@type': 'Vi...
6865  http://schema.org  ...  {'@context': 'http://schema.org', '@type': 'Vi...
6866  http://schema.org  ...  {'@context': 'http://schema.org', '@type': 'Vi...
6867  http://schema.org  ...  {'@context': 'http://schema.org', '@type': 'Vi...
6868  http://schema.org  ...  {'@context': 'http://schema.org', '@type': 'Vi...

[6869 rows x 15 columns]


print(df.columns)
Index(['@context', '@type', 'headline', 'description', 'articleBody',
       'articleSection', 'url', 'mainEntityOfPage', 'datePublished',
       'dateModified', 'author', 'publisher', 'image', 'keywords', 'video'],
      dtype='object')