1. 用正则表达式判定邮箱是否输入正确。
    import re
    mail = '''11040aaAAs3@qq.com'''
    if re.match('^[0-9a-zA-Z]{0,20}@[[0-9a-zA-Z]{0,10}\.[a-zA-Z]{1,3}$', mail):
        print('right')
    else:
        print('wrong')
    

      

  2.  用正则表达式识别出全部电话号码。
import re
str = '''学校办公室:020-82876130   招生电话:020-82872773'''
a=re.findall('(\d{3,4})-(\d{6,8})', str)
print(a)

  

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)

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

 

import re
Url = ' http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0401/8888.html'
newsId = re.search('\_(.*).html', Url).group(1).split('/')[-1]
print(newsId)

 

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 getNewsDetail(newsUrl):
    res = requests.get(newsUrl)
    res.encoding = 'utf-8'
    soupd = BeautifulSoup(res.text, 'html.parser')
    title=soupd.select('.show-title')[0].text
    info=soupd.select('.show-info')[0].text
    time=datetime.strptime(info.lstrip('发布时间:')[0:19], '%Y-%m-%d %H:%M:%S')
    if  info.find('来源:')>0:
        zy=info[info.find('来源:'):].split()[0].lstrip('来源:')
    else:
        zy='none'
    click=getClickCount(newsUrl)
    print(time,title,zy,click)

  

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. 获取全部新闻列表页的全部新闻详情。

pageUrl='http://news.gzcc.cn/html/xiaoyuanxinwen/'
n=getPageN()for i in range(1,n+1):
    print(i)
    listPageUrl='http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i)
    getListPage(listPageUrl)