当前位置: 首页 > news >正文

计算机毕业论文代做网站seo优化排名公司

计算机毕业论文代做网站,seo优化排名公司,做微信公众号整合网站,发布网站搭建教程爬虫:红网网站, 获取当月指定关键词新闻,并存储到CSV文件 V1.0 目标网站:红网 爬取目的:为了获取某一地区更全面的在红网已发布的宣传新闻稿,同时也让自己的工作更便捷 环境:Pycharm2021&#…

爬虫:红网网站, 获取当月指定关键词新闻,并存储到CSV文件 V1.0

目标网站:红网

爬取目的:为了获取某一地区更全面的在红网已发布的宣传新闻稿,同时也让自己的工作更便捷

环境:Pycharm2021,Python3.10,

安装的包:requests,csv,bs4,datetime

代码如下:(代码中附详细解析)

后续会不断完善,会出界面版,提高大家易用性;同时修改完善代码,设置为可指定获取的时间段的新闻稿。也会陆续更新其他新闻平台的新闻获取爬虫。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2024/3/25 23:05
# @Author : LanXiaoFang
# @Site : 
# @File : redNet.py
# @Software: PyCharm
import csvimport requests
from bs4 import BeautifulSoup
import datetimeheader = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8','Accept - Encoding': 'gzip, deflate, br',"Accept - Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",'Connection': "keep - alive",'Referer': 'https://news-search.rednet.cn/Search?q=%E5%8F%8C%E7%89%8C','User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:124.0) Gecko/20100101 Firefox/124.0","Cookie": "wdcid=7486a2c50eaf8af8; Hm_lvt_c96b65e9975fa39afbd5e90222af5f39=1711378746,1711528844; Hm_lvt_aaecf8414f59c3fb0127932014cf53c7=1711378746,1711528844; __jsluid_s=56e0acf3607072cce852b9d4fc556f54; Hm_lpvt_c96b65e9975fa39afbd5e90222af5f39=1711528844; Hm_lpvt_aaecf8414f59c3fb0127932014cf53c7=1711528844; __jsl_clearance_s=1711530480.242|1|%2F%2BG2WNMEpLXiwlUgRr2hiMkP%2BMg%3D","Upgrade-Insecure-Requests": "1",
}def get_all_indexes(s, char):return [i for i, c in enumerate(s) if c == char]# 获取系统时间
now = datetime.datetime.now()
year = now.year  # 年
month = now.month  # 月
day = now.day  # 日# 创建CSV文件并写入头部信息
with open(str(month) + 'MTitleSP.csv', 'w', newline='', encoding='utf-8') as csvfile:writer = csv.writer(csvfile)writer.writerow(['序号', '新闻名称', '新闻来源', '媒体级别', '发布日期', '原文链接'])  # 根据实际情况定义列名
with open(str(month) + 'MTitleNSP.csv', 'w', newline='', encoding='utf-8') as csvfile:writer = csv.writer(csvfile)writer.writerow(['序号', '新闻名称', '新闻来源', '媒体级别', '发布日期', '原文链接'])  # 根据实际情况定义列名article_no_sp = 1  # 用于计在标题含指定区域的存储的表中的数据的序号
article_no = 1  # 用于计在标题不含但内容含指定区域的存储的表中的数据的序号
get_go = 0  # 获取第几页开始的数据,现在是0开始
count = 0  # 用于计算总共爬取的新闻数量
area = '双牌'  # 爬取指定区域的文章# 相当于满足条件就是一直循环
while get_go >= 0:url = 'https://news-search.rednet.cn/Search?q=%E5%8F%8C%E7%89%8C&s=0&o=1&r=0&p=' + str(get_go)print(url)html = requests.get(url, headers=header)html.encoding = 'utf-8'get_go += 1if html.status_code == 200:soups = BeautifulSoup(html.text, 'html.parser')article_info = soups.find_all('div', class_='result')# print(len(article_info), '\n')for i in article_info:result_info = i.find_all('div', class_='result-info')station_source = result_info[0].select('span')  # 选择result_info下的所有span标签station_info = station_source[0].text  # 文章发布站点source_info = station_source[1].text  # 文章来源print(station_info, source_info)# print(i.find_all('div', class_='title'), '\n')title_info = i.find_all('div', class_='title')# 文章链接article_href = title_info[0].a.get('href')if station_info[3:] == area + "新闻网":# print("双牌新闻网文章链接:", article_href, "---------", "https://moment.rednet.cn/pc" + article_href[22:])article_href = "https://moment.rednet.cn/pc" + article_href[22:]# 修改文章来源为红网时刻if 'rednet' in article_href:source_info = "红网"if 'moment.rednet' in article_href:source_info = "红网时刻"if '来源' in source_info:source_info = station_info[3:]# 文章标题article_title = title_info[0].h3.text# 获取发布时间article_up_time = title_info[0].span.text# 把显示为进入和昨天的时间,改为具体的日期if article_up_time == '今天':article_up_time = str(year) + '.' + str(month) + '.' + str(day)elif article_up_time == '昨天':article_up_time = str(year) + '.' + str(month) + '.' + str(day - 1)# 修改时间显示格式,-替换为.else:# article_up_time = article_up_time[:4] + '.' + article_up_time[5:7] + '.' + article_up_time[8:10] + '.'article_up_time = article_up_time.replace('-', '.')count += 1print(count, '----新闻名称', article_title, '文章来源', source_info, '发布日期', article_up_time, '原文链接',article_href)# 得到这篇文章发布的月份all_index = get_all_indexes(article_up_time, '.')article_up_time_month = article_up_time[all_index[0] + 1:all_index[1]]# 只要本月的,如果获取到的文章是本月之前的则不再获取,退出循环if int(article_up_time_month) < month:print('已经不是这个月的啦', int(article_up_time_month), month)get_go = -1break# 把数据存入表格 根据标题是否含有双牌两个字 分开存储if area in article_title:# 这个是标题含有双牌的with open(str(month) + 'MTitleSP.csv', 'a', newline='', encoding='utf-8') as csvfile:writer = csv.writer(csvfile)writer.writerow([article_no_sp, article_title, source_info, '省级', article_up_time, article_href])article_no_sp += 1else:# 这个是标题不含但是内容含有双牌的with open(str(month) + 'MTitleNSP.csv', 'a', newline='', encoding='utf-8') as csvfile:writer = csv.writer(csvfile)writer.writerow([article_no, article_title, '省级', source_info, article_up_time, article_href])article_no += 1

由于现在是2024年4月1日 13:04,文章更新的本月的不多。

运行结果如下:

  

http://www.mmbaike.com/news/22745.html

相关文章:

  • 沈阳城乡建设委员会网站百度排名工具
  • 做pc网站最大分辨率友情链接怎么连
  • PHP 5+MySQL动态网站开发指南为什么中国禁止谷歌浏览器
  • 做餐饮的网站最火的网络推广平台
  • 视频投票网站怎么做的淘宝运营培训课程
  • 做网站的5要素上海怎么做seo推广
  • 佛山专业的网页制作seo整站网站推广优化排名
  • wordpress怎么修改网站标题河南推广网站
  • 微营销网站建设舆情分析报告
  • 湖南产品网络营销推荐咨询seo入门教程
  • 公司品牌flash网站挖掘关键词的工具
  • 上海网站建设天锐科技谷歌搜索引擎免费入口 台湾
  • 网站安全建设进展情况最近营销热点
  • wordpress.exeseo搜索优化公司排名
  • 帮人做网站的公司seo的基本工作内容
  • 广州做企业网站网络推广合作协议范本
  • 中国做的比较好的网站有哪些手机百度账号登录个人中心
  • 网页设计的技术有哪些南昌百度搜索排名优化
  • 莆田网站建设设计潍坊百度关键词优化
  • 亚马逊卖家做自己网站电商运营助理
  • 网站关键词如何设置网站建设与管理是干什么的
  • 做网站后开办会员科学新概念外链平台
  • 怎么做微信小说网站聊城网站推广公司
  • 宁夏建设网站58百度搜索引擎
  • 婚纱网站html模板微信小程序开发
  • 软件网站技术开发公司怎么看关键词的搜索量
  • 网罗天下做网站靠谱吗百度网盘电脑网页版
  • wordpress添加产品图seo综合诊断工具
  • 全球b2b网站排名seo的优点
  • wordpress 二次开发 pdfseo怎么去优化