码迷,mamicode.com
首页 > 其他好文 > 详细

爬取所有校园新闻

时间:2017-10-12 13:02:47      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:tcl   split   request   sele   encoding   orm   class   code   sel   

from datetime import datetime
import requests
from bs4 import BeautifulSoup
import re
html=http://news.gzcc.cn/html/xiaoyuanxinwen/
res = requests.get(html)
res.encoding=utf-8
soup = BeautifulSoup(res.text,html.parser)
def getdetail(htm):
    resd = requests.get(htm)
    resd.encoding=utf-8
    soupd = BeautifulSoup(resd.text,html.parser)
    return soupd.select(.show-content)[0].text
def getclick(newsurl):
    id =re.match(http://news.gzcc.cn/html/2017/xiaoyuanxinwen_(.*).html,newsurl).groups()[0].split(/)[1]
    clickurl="http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80".format(id)
    click=int(requests.get(clickurl).text.split(.)[-1].lstrip("html(‘").rstrip("‘);"))
    return(click)
for news in soup.select(li):
     if len(news.select(.news-list-title))>0:
         htm = news.select(a)[0][href]
         print (news.select(a)[0][href])
         print (news.select(.news-list-title)[0].text)
         time=news.select(.news-list-info)[0].contents[0].text
         print (datetime.strptime(time,%Y-%m-%d))
         print (news.select(.news-list-info)[0].contents[1].text)
         detail =  getdetail(htm)
         click=getclick(htm)
         print(detail)
         print(click)
  
    #click=requests.get(http://oa.gzcc.cn/api.php?op=count&id=8249&modelid=80).text.split(.)[-1].lstrip("html(‘").rstrip("‘);")
    #print (click)
from datetime import datetime
import requests
from bs4 import BeautifulSoup
import re


def getclick(newsurl):
    id =re.match(http://news.gzcc.cn/html/2017/xiaoyuanxinwen_(.*).html,newsurl).groups()[0].split(/)[1]
    clickurl="http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80".format(id)
    click=int(requests.get(clickurl).text.split(.)[-1].lstrip("html(‘").rstrip("‘);"))
    return(click)
def getdetail(listurl):
    resd = requests.get(listurl)
    resd.encoding=utf-8
    soupd = BeautifulSoup(resd.text,html.parser)
    soupd.select(.show-content)[0].text
    for news in soup.select(li):
        if len(news.select(.news-list-title))>0:
           h = news.select(a)[0][href]
           print (news.select(a)[0][href])
           print (news.select(.news-list-title)[0].text)
           time=news.select(.news-list-info)[0].contents[0].text
           print (datetime.strptime(time,%Y-%m-%d))
           print (news.select(.news-list-info)[0].contents[1].text)
           detail = soupd.select(.show-content)[0].text
           click=getclick(h)
           print(detail)
           print(click)
getdetail=(http://news.gzcc.cn/html/xiaoyuanxinwen/index.html) 
res = requests.get("http://news.gzcc.cn/html/xiaoyuanxinwen/")
res.encoding=utf-8
soup = BeautifulSoup(res.text,html.parser)
pages=int(soup.select(.a1)[0].text.rstrip())//10+1  #计算页
print (pages)
for i in range(2,5):
    list=http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html.format(i)
    getdetail(list)

 

爬取所有校园新闻

标签:tcl   split   request   sele   encoding   orm   class   code   sel   

原文地址:http://www.cnblogs.com/wlh353/p/7655433.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!