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

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=" 1561515818"  
m=re.findall(r"1\d{10}",text)    
if m:  
    print m  
else:  
    print 'not match' 

  

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

import re
news = '''It's true that we don't know what we've got until we lose it, but it's also true that we don't know what we've been losing until it arrives. '''
new=re.split('[\s,.?!\-]+',news)
print(new)

 

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

news = "Facebook? Informs Data Leak Victims Whether They " \
       "Need To Burn Down House, Cut Off Fingerprints, Start Anew,"
word = re.split("[\s,.?\-]+", news)
print(word)

  

5. 生成点击次数的Request URL

import requests
res = requests.get('http://oa.gzcc.cn/api.php?op=count&id=9122&modelid=80')
res.encoding = 'utf-8'
b=res.text.split('.html')[-1].lstrip("(')").rstrip("');")
print(b)

  

6. 获取点击次数

import requests
import re
newsUrl = 'http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0329/9122.html'
newsId=re.search('\_(.*).html',newsUrl).group(1).split('/')[-1]
res = requests.get('http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80'.format(newsId))
b=res.text.split('.html')[-1].lstrip("(')").rstrip("');")
print(b)

  

 

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

def getClickCount(newsUrl):
newsId=re.search('\_(.*).html',newsUrl).group(1).split('/')[-1]
res = requests.get('http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80'.format(newsId))
b=res.text.split('.html')[-1].lstrip("(')").rstrip("');")
print(b)

  

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

def getNewDetail(newsUrl):
    res=requests.get(newsUrl)
    res.encoding='utf-8'
    soup=BeautifulSoup(res.text,'html.parser')
    global soupDetail

    for news in soup.select('li'):
        if len(news.select('.news-list-title'))>0:
            pert=news.select('.news-list-title')[0].text              #pertitle   每则新闻题目
            perdt=news.select('.news-list-info')[0].contents[0].text  #perDetail  每则新闻详细内容
            perhref=news.select('a')[0].attrs['href']                 #perHref    每则新闻源链接

            # ————————————爬取子页面内容——————————————————————————
            global soupDetail
            perdetail=requests.get(perhref)
            perdetail.encoding='utf-8'
            soupDetail=BeautifulSoup(perdetail.text,'html.parser')
            textContent=soupDetail.select('#content')[0].text

            #————————————输出内容——————————————
            print('题目:',pert)
            print('发布时间:',perdt)
            print('源页面:',perhref)
            print('正文内容:',textContent)
            break

  

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:
            g = news.select('a')[0].attrs['href']
            print(g)
            getNewsDetail(g)

  

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

def getPageN():
    res = requests.get('http://news.gzcc.cn/html/xiaoyuanxinwen/')
    res.encoding = 'utf-8'
    soup = BeautifulSoup(res.text, 'html.parser')
    pagenumber=int(soup.select('.a1')[0].text.rstrip('条'))
    page = pagenumber//10+1
    return page

  

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

def crawlOnePageSchoolNews(page_url):
    res = requests.get(page_url)
    res.encoding = 'UTF-8'
    soup = BeautifulSoup(res.text, 'html.parser')
    news = soup.select('.news-list > li')
    for n in news:
        # print(n)
        print('**' * 5 + '列表页信息' + '**' * 10)
        print('新闻链接:' + n.a.attrs['href'])
        print('新闻标题:' + n.select('.news-list-title')[0].text)
        print('新闻描述:' + n.a.select('.news-list-description')[0].text)
        print('新闻时间:' + n.a.select('.news-list-info > span')[0].text)
        print('新闻来源:' + n.a.select('.news-list-info > span')[1].text)
        getNewDetail(n.a.attrs['href'])

  

posted on 2018-04-11 20:01  244谭颖欣  阅读(100)  评论(0编辑  收藏  举报