爬取校园网新闻首页的新闻

复制代码

import requests
from bs4 import BeautifulSoup
def get_soup(url):
    req = requests.get(url)
    req.encoding = 'utf-8'
    soup = BeautifulSoup(req.text, 'html.parser')
    return soup
soup = get_soup('http://news.gzcc.cn/html/xiaoyuanxinwen/')
li_list = soup.select('li')
title = list()
a = list()
for new in li_list:
    if(len(new.select('.news-list-text'))>0):
        title.append(new.select('.news-list-text')[0].select('.news-list-title')[0].text)
        a.append(new.a.attrs['href'])
info_list = list()
con_list = list()
delw = ['\r','\n','\u3000','\xa0']
for curl in a:
    con_soup = get_soup(curl)
    con_list.append(con_soup.select('#content')[0].text)
    info_list.append(con_soup.select('.show-info')[0].text.split("\xa0\xa0"))
cs = list()
for i in range(len(con_list)):
    cs.append(''.join(con_list[0]))
for i in range(len(info_list)):
    print(title[i] + "        " + a[i])
    for j in range(len(info_list[i])):
        if(len(info_list[i][j])>0 and info_list[i][j]!=' '):
            print(info_list[i][j])
    print(cs[i])
复制代码
posted @ 2018-04-04 16:21  153刘宇  阅读(309)  评论(0编辑  收藏  举报