爬取校园新闻首页的新闻


1. 用requests库和BeautifulSoup库,爬取校园新闻首页新闻的标题、链接、正文。

url = "http://news.gzcc.cn/html/xiaoyuanxinwen/"
res = requests.get(url);
res.encoding = "utf-8"
soup = BeautifulSoup(res.text,"html.parser");
 
for news in soup.select("li"):
    if len(news.select(".news-list-title"))>0:  #排除为空的li
        time = news.select(".news-list-info")[0].contents[0].text
        title = news.select(".news-list-title")[0].text
        description = news.select(".news-list-description")[0].text
        a = news.select('a')[0].attrs['href']
        detail_res = requests.get(a)
        detail_res.encoding = "utf-8"
        detail_soup = BeautifulSoup(detail_res.text, "html.parser")
        print(detail_soup.select("#content")[0].text)#正文
        print(time, title, description, a)
        content = detail_soup.select("#content")[0].text
        info = detail_soup.select(".show-info")[0].text
        date_time = info.lstrip('发布时间:')[:19]
        print(info)
        break

  

  2. 分析字符串,获取每篇新闻的发布时间,作者,来源,摄影等信息。

info = '发布时间:2018-04-01 11:57:00      作者:陈流芳  审核:权麟春  来源:马克思主义学院      点击:次'
detail_time = info.lstrip('发布时间:')[:19]
sh = info[info.find("审核"):].split()[0].lstrip('审核:')
print(detail_time,sh)

  3. 将其中的发布时间由str转换成datetime类型。

# 获取当前的时间
now_time = datetime.now();
now_time.year
# 将字符串转化为时间
print(datetime.strptime(date_time,"%Y-%m-%d %H:%M:%S"))
# 将时间转化为字符串
print(now_time.strftime('%Y\%m\%d'))

  4. 将完整的代码及运行结果截图发布在作业上。

import requests
from bs4 import BeautifulSoup
from datetime import datetime
 
url = "http://news.gzcc.cn/html/xiaoyuanxinwen/"
res = requests.get(url);
res.encoding = "utf-8"
soup = BeautifulSoup(res.text,"html.parser");
 
for news in soup.select("li"):
    if len(news.select(".news-list-title"))>0:  #排除为空的li
        time = news.select(".news-list-info")[0].contents[0].text
        title = news.select(".news-list-title")[0].text
        description = news.select(".news-list-description")[0].text
        a = news.select('a')[0].attrs['href']
        detail_res = requests.get(a)
        detail_res.encoding = "utf-8"
        detail_soup = BeautifulSoup(detail_res.text, "html.parser")
        print(detail_soup.select("#content")[0].text)#正文
 
        print(time, title, description, a)
 
        content = detail_soup.select("#content")[0].text
        info = detail_soup.select(".show-info")[0].text
        date_time = info.lstrip('发布时间:')[:19]
        print(info)
        break
 
info = '发布时间:2018-04-01 11:57:00      作者:陈流芳  审核:权麟春  来源:马克思主义学院      点击:次'
detail_time = info.lstrip('发布时间:')[:19]
sh = info[info.find("审核"):].split()[0].lstrip('审核:')
print(detail_time,sh)
 
 
# # 多个名字查找作者
info1 = '发布时间:2018-04-01 11:57:00      作者:陈流芳 许健杰  审核:权麟春   来源:马克思主义学院    点击:次 '
info1 = info1[info1.find("作者"):info1.find('审核:')].lstrip('作者:').split()[1]
print(info1)
 
# 获取当前的时间
now_time = datetime.now();
now_time.year
 
# 将字符串转化为时间
print(datetime.strptime(date_time,"%Y-%m-%d %H:%M:%S"))
 
# 将时间转化为字符串
print(now_time.strftime('%Y\%m\%d'))

  

优质内容筛选与推荐>>
1、论文阅读 - Collaborative Filtering Survey
2、Python+selenium自动循环送贺卡
3、LeetCode 10. Regular Expression Matching
4、循环
5、MySQL基准测试和sysbench工具


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn