用正则表达式,取得点击次数,函数抽离

学会使用正则表达式

1. 用正则表达式判定邮箱是否输入正确。

import re
def validateEmail(email):
    if len(email) > 7:
        if re.match("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", email) != None:
            return 1
    return 0

2. 用正则表达式识别出全部电话号码。

import re
text="123456 1847854 13257872936 135938975 1a2b3c a123456"
m=re.findall(r"1\d{10}",text)
if m:
    print (m)
else:
    print('not match')

3. 用正则表达式进行英文分词。re.split('',news)

news = '''How old are you?'''
print(re.split('[\s,.?\-]', news))

4. 使用正则表达式取得新闻编号

url = "http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1225/8854.html"
newsId = re.findall("\_(.*).html", url)[0].split("/")[-1]
print(newsId)

5. 生成点击次数的Request URL

Rurl = "http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80".format(newsId)
print(Rurl)

6. 获取点击次数

res = requests.get("http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80".format(newsId))
print(int(res.text.split(".html")[-1].lstrip("('").rsplit("');")[0]))

7. 将456步骤定义成一个函数 def getClickCount(newsUrl):

def getClickCount():
    url = "http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1225/8854.html"
    newsId = re.findall("\_(.*).html", url)[0].split("/")[-1]
    res1 = requests.get("http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80".format(newsId))
    return int(res1.text.split(".html")[-1].lstrip("('").rsplit("');")[0])
print(getClickCount())

8. 将获取新闻详情的代码定义成一个函数 def getNewDetail(newsUrl):

def getNewDetail():
    detail_res = requests.get("http://news.gzcc.cn/html/2017/xiaoyuanxinwen_1225/8854.html")
    detail_res.encoding = "utf-8"
    detail_soup = BeautifulSoup(detail_res.text, "html.parser")
    content = detail_soup.select("#content")[0].text
    info = detail_soup.select(".show-info")[0].text
    return content, info
print(getNewDetail())

9. 取出一个新闻列表页的全部新闻 包装成函数def getListPage(pageUrl):

def getListPage(pageUrl):
    res = requests.get(pageUrl)
    res.encoding = 'utf-8'
    soup = BeautifulSoup(res.text, 'html.parser')
    for news in soup.select('li'):
        if len(news.select('.news-list-title')) > 0:
            d = news.select('.news-list-info')[0].contents[0].text
            t = news.select('.news-list-title')[0].text
            href = news.select('a')[0].attrs['href']
            clicknum=getClickCount(href)
            print(t,d,href)
            resd = requests.get(href)
            resd.encoding = 'utf-8'
            soupd = BeautifulSoup(resd.text, 'html.parser')
            infod = soupd.select('.show-info')[0].text
            dt = infod.lstrip('发布时间:')[:19]
            ts = soupd.select('#content')[0].text
            dati = datetime.strptime(dt, '%Y-%m-%d %H:%M:%S')
            ther = infod[infod.find('作者:'):infod.find('审核:')].split()[0].lstrip('作者:')
            check = infod[infod.find('审核:') + 3:infod.find('来源')]
            source = infod[infod.find('来源:'):].split()[0].lstrip('来源:')
            photo = infod[infod.find('摄影:'):].split()[0].lstrip('摄影:')
            print(dati,ther,check,source,photo,clicknum)

10. 获取总的新闻篇数,算出新闻总页数包装成函数def getPageN():

def getPageN():
    res=requests.get('http://news.gzcc.cn/html/xiaoyuanxinwen/')
    res.encoding='utf-8'
    soup=BeautifulSoup(res.text,'html.parser')
    news=soup.select('#pages')[0].select('a')[0].text.rstrip('条')
    print(news)
 
getListPage('http://news.gzcc.cn/html/xiaoyuanxinwen/')

11. 获取全部新闻列表页的全部新闻详情。

getListPage(newsUrl)
n=getPageN()
for i in range(n,n+1):
    print(i)
    pageUrl='http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i)
    getListPage(pageUrl)
posted @ 2018-04-11 14:40  227温立恒  阅读(143)  评论(0编辑  收藏  举报