用python爬取今日说法每期数据

实验目的

主要是获取2021年今日说法每期节目主要内容及时间
今日说法的网址为:http://tv.cctv.com/lm/jrsf/index.shtml
当时怎么写的思路有点不太记得了,先把代码贴上,后续有时间再补上。

代码

import xlwt
import re

import requests


# url = "https://tv.cctv.com/lm/jrsf/index.shtml"

def get_data(page):
    url = 'https://api.cntv.cn/NewVideo/getVideoListByColumn?id=TOPC145146466500891' \
          '4&n=1000&sort=desc&p={pageNo}&mode=0&serviceId=tvcctv&cb=Callback'.format(pageNo=page)
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
                      '(KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36'
    }
    response = requests.get(url=url, headers=headers)
    return response.text
    # print(response.text)


if __name__ == "__main__":

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

    book = xlwt.Workbook(encoding='utf-8', style_compression=0)
    sheet = book.add_sheet('今日说法', cell_overwrite_ok=True)

    count = 0
    for page in range(1,5):
        page_content = get_data(page)
        obj = re.compile(r'url":"(.*?.shtml)"', re.S)
        imgUrl = re.findall(obj, page_content.replace('\\', ''))
        for i in range(len(imgUrl)):
            resp = requests.get(url=imgUrl[i], headers=headers)
            resp.encoding = 'utf-8'

            obj2 = re.compile(r'更新时间:(.*?)

', re.S) time = re.findall(obj2, resp.text) obj3 = re.compile(r'视频简介:(.*?)

', re.S) jianjie = re.findall(obj3, resp.text) content = [] content.append(time) content.append(jianjie) for j in range(2): sheet.write(count, j, content[j]) count+=1 book.save("./data_5.xls")

实验结果

用python爬取今日说法每期数据_第1张图片

总结

到此这篇关于用python爬取今日说法每期数据的文章就介绍到这了,更多相关python爬取今日说法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(用python爬取今日说法每期数据)