python - 采集 新浪新闻-国内-最新消息 转为xlsx
发布日期:2021-06-30 19:51:12
浏览次数:2
分类:技术文章
本文共 1436 字,大约阅读时间需要 4 分钟。
# coding: utf-8# In[63]:import requestsfrom pyquery import PyQuery as pqresponse = requests.get(url='http://news.sina.com.cn/china/')response.encoding = 'utf-8'html = response.textdoc = pq(html)news_list = []for item in doc('.news-item').items(): news_url = item.find('h2 > a').attr('href') if news_url: news_list.append(get_article(news_url))def get_article(news_url): response = requests.get(url=news_url) response.encoding = 'utf-8' html = response.text doc = pq(html) msg = {} msg['title'] = doc.find('#artibodyTitle').text() msg['content'] = ''.join(doc.find('#artibody > p').text().split()) msg['time'] = doc.find('#navtimeSource').text() msg['keyword'] = doc.find('.article-keywords > a').text() return msgnews_list# In[64]:import pandas as pddf = pd.DataFrame(news_list)df.head()# In[65]:df['keyword'] = df['keyword'].map(lambda e : e.split())df.head()# In[66]:df['time'].map(lambda e : e.split()).head()# In[67]:df['time'].str.extract('(\d+年\d+月\d+日\d+:\d+)\s+(\w+)').head()# In[68]:df[['datetime', 'from']] = df['time'].str.extract('(\d+年\d+月\d+日\d+:\d+)\s+(\w+)')df.head()# In[69]:df['datetime'] = pd.to_datetime(df['datetime'], format = '%Y年%m月%d日%H:%M')df['datetime'].head()# In[70]:df['datetime'].map(lambda e : (e.year, e.month, e.day)).head()# In[71]:del df['time']df.head()# In[72]:df = df[['from', 'title', 'content', 'keyword', 'datetime']]# In[73]:df.to_excel('news.xlsx')
转载地址:https://lipenglin.blog.csdn.net/article/details/78002887 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
很好
[***.229.124.182]2024年04月05日 10时43分26秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
research gap
2019-04-30
pytorch训练cifar10数据集查看各个种类图片的准确率
2019-04-30
Python鼠标点击图片,获取点击点的像素坐标
2019-04-30
路径规划(一) —— 环境描述(Grid Map & Feature Map) & 全局路径规划(最优路径规划(Dijkstra&A*star) & 概率路径规划(PRM&RRT))
2019-04-30
RRT算法(快速拓展随机树)的Python实现
2019-04-30
D*算法
2019-04-30
强化学习(四) —— Actor-Critic演员评论家 & code
2019-04-30
RESTful API
2019-04-30
优化算法(四)——粒子群优化算法(PSO)
2019-04-30
数据在Oracle中的存储
2019-04-30
轨迹规划 trajectory planning
2019-04-30
AGV自动导引运输车
2019-04-30
Trie树(字典树)
2019-04-30