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

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

时间:2018-04-11 21:46:56      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:sel   dal   学院   发布   .gz   n+1   requests   list   AC   

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

import re
r =‘^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$‘
e =‘549571966@qq.com‘
if re.match(r,e):
    print(re.match(r,e).group(0))
else:
    print(‘error‘)

  

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

import re
str =‘‘‘020-82876130 版权所有:广州商学院 地址:广州市黄埔区九龙大道206号
学校办公室:020-82876130 招生电话:020-82872773
校外办公室0724-4263864 粤公网安备 44011602000060号    粤ICP备15103669号
‘‘‘
number=re.findall(‘(\d{3,4})-(\d{6,8})‘,str)
print(number)

  

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

import re
news = ‘‘‘Failure is probably the fortification in your pole. It is like a peek your wallet as the thief, when you are thinking how to spend several hard-won lepta, when you are wondering whether new money, it has laid background.‘‘‘
word = re.split(‘[\s,.?\-]+‘,news)
print(word)

  

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

import re
newsUrl = ‘http://news.gzcc.cn/html/2017/xiaoyuanxinwen_095/8249.html‘
num=re.search(‘\_(.*).html‘,newsUrl).group(1)
print(num)

  

5. 生成点击次数的Request URL

import re
newUrl = "http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0404/9183.html"
newsId = re.findall("\_(.*).html",newUrl)[0].split("/")[-1];
RequestUrl = "http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80".format(newsId)
print(RequestUrl)

  

6. 获取点击次数

import re
import requests
newUrl = "http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0404/9183.html"
newsId = re.findall("\_(.*).html",newUrl)[0].split("/")[-1];
RequestUrl = "http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80".format(newsId)
res = requests.get(RequestUrl);
times=int(res.text.split(‘.html‘)[-1].lstrip("(‘)").rstrip("‘);"))
print(times)

  

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

import re
import requests
def getClickCount(newsUrl):
    newsId = re.findall("\_(.*).html",newsUrl)[0].split("/")[-1];
    RequestUrl = "http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80".format(newsId)
    res = requests.get(RequestUrl);
    times=int(res.text.split(‘.html‘)[-1].lstrip("(‘)").rstrip("‘);"))
    return times
time=getClickCount("http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0404/9183.html")
print(time)

  

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

import requests
from bs4 import BeautifulSoup
def getNewDetail(newsUrl):
    res = requests.get(newsUrl)
    res.encoding = ‘utf-8‘
    soup = BeautifulSoup(res.text, ‘html.parser‘)
    print(soup.select("#content")[0].text)  # 正文
    info = soup.select(".show-info")[0].text
    time = info.lstrip(‘发布时间:‘)[:19]
    # 作者
    if info.find(‘作者:‘) > 0:
        author = info[info.find(‘作者:‘):info.find(‘审核:‘)].lstrip(‘作者:‘).split()[0]
    else:
        author = ‘none‘;
    print(author)
getNewDetail(‘http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0404/9183.html‘)

  

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

import re
import requests
from bs4 import BeautifulSoup
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:
            time = news.select(".news-list-info")[0].contents[0].text
            title = news.select(".news-list-title")[0].text
            description = news.select(".news-list-description")[0].text
            url = news.select(‘a‘)[0].attrs[‘href‘]
            print(time, title, description,url)
getListPage(‘http://news.gzcc.cn/html/xiaoyuanxinwen/‘)

  

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

import re
import requests
from bs4 import BeautifulSoup
def getPageN():
    res = requests.get(‘http://news.gzcc.cn/html/xiaoyuanxinwen/‘)
    res.encoding = "utf-8"
    soup = BeautifulSoup(res.text, ‘html.parser‘)
    n = int(soup.select(‘#pages‘)[0].select(‘a‘)[0].text.rstrip(‘条‘))
    return (n // 10 + 1)

  

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

import re
import requests
from bs4 import BeautifulSoup
 
#获取点击次数
def getClickCount(newsUrl):
    newsId = re.findall("\_(.*).html",newsUrl)[0].split("/")[-1];
    RequestUrl = "http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80".format(newsId)
    res = requests.get(RequestUrl);
    times=int(res.text.split(‘.html‘)[-1].lstrip("(‘)").rstrip("‘);"))
    return times
 
#获取新闻详细信息
def getNewDetail(newsUrl):
    res = requests.get(newsUrl)
    res.encoding = ‘utf-8‘
    soup = BeautifulSoup(res.text, ‘html.parser‘)
    # print(soup.select("#content")[0].text)  # 正文
    info = soup.select(".show-info")[0].text
    time = info.lstrip(‘发布时间:‘)[:19]
    # 作者
    if info.find(‘作者:‘) > 0:
       author = info[info.find(‘作者:‘):info.find(‘审核:‘)].lstrip(‘作者:‘).split()[0]
    else:
        author= ‘none‘;
    print("作者:"+author+" "+"发布时间"+time)
 
#获取该页新闻的信息
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:
            time = news.select(".news-list-info")[0].contents[0].text
            title = news.select(".news-list-title")[0].text
            description = news.select(".news-list-description")[0].text
            url = news.select(‘a‘)[0].attrs[‘href‘]
            print(time+" "+title+" "+description+" "+url)
            a=getClickCount(url);
            print("点击"+str(a)+"次")
            getNewDetail(url)
 
 
#获取页数
def getPageN():
    res = requests.get(‘http://news.gzcc.cn/html/xiaoyuanxinwen/‘)
    res.encoding = "utf-8"
    soup = BeautifulSoup(res.text, ‘html.parser‘)
    n = int(soup.select(‘#pages‘)[0].select(‘a‘)[0].text.rstrip(‘条‘))
    return (n // 10 + 1)
 
n=getPageN();
for i in range(1,n+1):
    if(i==1):
        newsurl = ‘http://news.gzcc.cn/html/xiaoyuanxinwen/‘
    else:
        newsurl = ‘http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html‘.format(i)
    getListPage(newsurl);

  技术分享图片

 

 

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

标签:sel   dal   学院   发布   .gz   n+1   requests   list   AC   

原文地址:https://www.cnblogs.com/ldg-01/p/8798519.html

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