Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

import requests import json import re from bs4 import BeautifulSoup from datetime import…

  1. User

    import requests import json import re from bs4 import BeautifulSoup from datetime import datetime,timedelta from typing import Union

    class Main():

    def __init__(self) -> None:
        pass
    
    def fetch(self, url) -> str:
        headers = {
            'accept':
            'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
            'user-agent': 'application/json, text/javascript, */*; q=0.01',
        }
        r = requests.get(url, headers=headers)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    
    def getDailyNews(self) -> list[dict[str, Union[str, list[dict[str, str]]]]]:
        # # yyyy-mm-dd 格式化当天日期
        # formatted_date = datetime.now().strftime("%Y-%m-%d")
    
        current_date = datetime.now()
    
        for i in range(365):
            new_date = current_date - timedelta(days=i)
            formatted_date =new_date.strftime("%Y-%m-%d")
            naviUrl = f'https://www.shobserver.com/staticsg/data/journal/{formatted_date}/navi.json'
        
    
        try:
            naviData = json.loads(self.fetch(naviUrl))
            newsPages = naviData["pages"]
            print(f'「解放日报」正在处理 {formatted_date} 的 {len(newsPages)} 版新闻...')
    
            news = []
            for newsPage in newsPages:
                pageName = newsPage["pname"]
                pageNo = newsPage["pnumber"]
                articleList = newsPage["articleList"]
                print(
                    f'「解放日报」{pageNo} 版 - {pageName} 共有 {len(articleList)} 条新闻')
                for article in articleList:
                    title = article["title"]
                    subtitle = article["subtitle"]
                    aid = article["id"]
    
                    # 使用正则丢弃 title 含有广告的文章
                    if re.search(r'广告', title):
                        continue
    
                    articleContent, articlePictures = self.getArticle(
                        formatted_date, pageNo, aid)
                    news.append({
                        "id": f'{formatted_date}_{pageNo}-{aid}',
                        "title": title,
                        "subtitle": subtitle,
                        "content": articleContent,
                        "pictures": articlePictures
                    })
    
            return news
    
        except Exception as e:
            print(f'「解放日报」新闻列表获取失败!\n{e}')
            return []
    
    def getArticle(self, date, pageNo, aid) -> tuple[str, list[object]]:
        articleUrl = f'https://www.shobserver.com/staticsg/data/journal/{date}/{pageNo}/article/{aid}.json'
    
        articleData = json.loads(self.fetch(articleUrl))["article"]
        articleContent = BeautifulSoup(articleData["content"], 'html.parser')
        # 转换 <br> 为 \n
        for br in articleContent.find_all("br"):
            br.replace_with("\n")
    
        articlePictures = []
        articlePictureJson = json.loads(articleData["pincurls"])
        for articlePicture in articlePictureJson:
            url = articlePicture["url"]
            name = articlePicture["name"]
            author = articlePicture["author"]
            ttile = articlePicture["ttile"]
            articlePictures.append({
                "url": url,
                "alt": ttile,
                "title": ttile,
                "source": name,
                "author": author
            })
    
        print(
            f'「解放日报」已解析 {pageNo} 版 - {articleData["title"]} | 字数 {len(articleContent)} | 图片 {len(articlePictures)} 张'
        )
        return articleContent.get_text(), articlePictures
    

    jfdaily_spider = Main()

    if name == 'main': spider = Main() news = spider.getDailyNews() print(news) 这个为什么就处理一天的数据

  2. Assistant

    这段代码中,由于循环的缩进有误,导致只处理最后一天的数据。要解决这个问题,需要将循环的范围移至正确的位置。

    以下为修正后的代码:

    class Main():
    
        def __init__(self) -> None:
            pass
    
        def fetch(self, url) -> str:
            headers = {
                'accept':
                'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
                'user-agent': 'application/json, text/javascript, */*; q=0.01',
            }
            r = requests.get(url, headers=headers)
            r.raise_for_status()
            r.encoding = r.apparent_encoding
            return r.text
    
        def getDailyNews(self) -> list[dict[str, Union[str, list[dict[str, str]]]]]:
            current_date = datetime.now()
    
            news = []
    
            for i in range(365):
                new_date = current_date - timedelta(days=i)
                formatted_date = new_date.strftime("%Y-%m-%d")
                naviUrl = f'https://www.shobserver.com/staticsg/data/journal/{formatted_date}/navi.json'
    
                try:
                    naviData = json.loads(self.fetch(naviUrl))
                    newsPages = naviData["pages"]
                    print(f'「解放日报」正在处理 {formatted_date} 的 {len(newsPages)} 版新闻...')
    
                    for newsPage in newsPages:
                        pageName = newsPage["pname"]
                        pageNo = newsPage["pnumber"]
                        articleList = newsPage["articleList"]
                        print(
                            f'「解放日报」{pageNo} 版 - {pageName} 共有 {len(articleList)} 条新闻')
    
                        for article in articleList:
                            title = article["title"]
                            subtitle = article["subtitle"]
                            aid = article["id"]
    
                            # 使用正则丢弃 title 含有广告的文章
                            if re.search(r'广告', title):
                                continue
    
                            articleContent, articlePictures = self.getArticle(
                                formatted_date, pageNo, aid)
                            news.append({
                                "id": f'{formatted_date}_{pageNo}-{aid}',
                                "title": title,
                                "subtitle": subtitle,
                                "content": articleContent,
                                "pictures": articlePictures
                            })
    
                except Exception as e:
                    print(f'「解放日报」新闻列表获取失败!\n{e}')
            
            return news
    
        def getArticle(self, date, pageNo, aid) -> tuple[str, list[object]]:
            articleUrl = f'https://www.shobserver.com/staticsg/data/journal/{date}/{pageNo}/article/{aid}.json'
    
            articleData = json.loads(self.fetch(articleUrl))["article"]
            articleContent = BeautifulSoup(articleData["content"], 'html.parser')
            # 转换 <br> 为 \n
            for br in articleContent.find_all("br"):
                br.replace_with("\n")
    
            articlePictures = []
            articlePictureJson = json.loads(articleData["pincurls"])
            for articlePicture in articlePictureJson:
                url = articlePicture["url"]
                name = articlePicture["name"]
                author = articlePicture["author"]
                ttile = articlePicture["ttile"]
                articlePictures.append({
                    "url": url,
                    "alt": ttile,
                    "title": ttile,
                    "source": name,
                    "author": author
                })
    
            print(
                f'「解放日报」已解析 {pageNo} 版 - {articleData["title"]} | 字数 {len(articleContent)} | 图片 {len(articlePictures)} 张'
            )
            return articleContent.get_text(), articlePictures
    

    这样修改后,getDailyNews()方法会处理每天的数据,并将所有新闻存储在列表news中。

  3. User

    上述代码在哪里解决的

  4. Assistant

    在上述代码中,解决了只处理最后一天数据的问题的地方是在getDailyNews()方法中的循环中。

    原来的代码中,循环的范围是在整个getDailyNews()方法之外,因此每次循环都会覆盖之前的结果,最终只返回了最后一天的数据。

    修改后的代码中,将循环范围移至正确的位置。将循环的范围改为从当前日期起往前推算365天,这样每次循环都会处理一个新的日期,然后将每天的新闻添加到列表news中。这样就能够处理每天的数据,并将所有新闻存储在列表中。最终返回的是包含所有新闻的列表。

  5. User

    将上述转换成EXCEL输出

  6. Assistant

    要将获取的新闻数据输出到Excel文件中,可以使用pandas库进行操作。以下是对代码进行修改后,将新闻数据输出到Excel文件的示例代码:

    import pandas as pd
    
    class Main():
        # ... 省略其他代码 ...
    
        def exportToExcel(self, news):
            data = {
                "ID": [],
                "标题": [],
                "副标题": [],
                "内容": [],
                "图片URL": [],
                "图片来源": [],
                "图片作者": []
            }
    
            for article in news:
                data["ID"].append(article["id"])
                data["标题"].append(article["title"])
                data["副标题"].append(article["subtitle"])
                data["内容"].append(article["content"])
    
                pictures = article["pictures"]
                if pictures:
                    picture = pictures[0]
                    data["图片URL"].append(picture["url"])
                    data["图片来源"].append(picture["source"])
                    data["图片作者"].append(picture["author"])
                else:
                    data["图片URL"].append("")
                    data["图片来源"].append("")
                    data["图片作者"].append("")
    
            df = pd.DataFrame(data)
            df.to_excel("news_data.xlsx", index=False)
    
    # 在主程序中调用 exportToExcel() 方法
    jfdaily_spider = Main()
    
    if __name__ == '__main__':
        spider = Main()
        news = spider.getDailyNews()
        spider.exportToExcel(news)
    

    在修改后的代码中,我添加了一个 exportToExcel() 方法来将新闻数据输出到Excel文件中。该方法会遍历新闻列表,并将新闻数据存储在一个字典中,然后使用 pandas 创建一个数据框(DataFrame),并将数据框导出到 news_data.xlsx Excel文件中。

    需要注意的是,为了运行代码,您需要确保已经安装了 pandas 库。可以使用以下命令安装该库:

    pip install pandas
    

    一旦安装完成,运行上述代码后,将会在当前工作目录下生成 news_data.xlsx 文件,其中包含获取的新闻数据。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.