zoukankan      html  css  js  c++  java
  • 爬取校园新闻首页的新闻

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

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

    import requests
    newsurl='http://news.gzcc.cn/html/xiaoyuanxinwen/'
    res = requests.get(newsurl) #返回response对象
    res.encoding='utf-8'
    from bs4 import BeautifulSoup
    soup = BeautifulSoup(res.text,'html.parser')
    #循环遍历打印
    for news in soup.select('li'):
        if len(news.select('.news-list-title'))>0:
            t=news.select('.news-list-title')[0].text
            d=news.select('.news-list-description')[0].text
            a=news.a.attrs['href']
            print(t,d,a)
    # 取出一条新闻的标题、链接、发布时间、来源
    print('标题:'+soup.select('.news-list-title')[0].text)
    print('链接:'+soup.select('a')[2]['href'])
    print('发布时间:'+soup.select('.news-list-info')[0].span.text)
    print('来源:'+soup.select('.news-list-info')[0].select('span')[1].text)

    结果如下:

  • 相关阅读:
    mysql注入小测试
    让函数返回指定值实用写法
    源码下载网址
    带宽
    九度oj 题目1080:进制转换
    九度oj 题目1079:手机键盘
    poj 3046 Ant Counting
    整数拆分问题
    poj 2229 Sumsets
    九度oj 题目1411:转圈
  • 原文地址:https://www.cnblogs.com/1103a/p/8718247.html
Copyright © 2011-2022 走看看