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

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

r = '^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$'
e = '980378507@qq.com'
s = '输入邮箱正确:'
if re.match(r,e):
    print(s,re.match(r,e).group(0))
else:
    print('error')

  

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

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

  

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

str1 = 'Aside from heavy casualties, the fire also caused property losses, Xi said.'
out = re.split(',|\s',str1)
print(out)

  

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

newId = re.search('\_(.*).html',newUrl).group(1).split('/')[-1]
print(newId)

  

5. 生成点击次数的Request URL

url = 'http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80'.format(newId)
print(url)

  

6. 获取点击次数

res = requests.get(url)
count = res.text.split(".html")[-1].lstrip("(')").rstrip("');")
print(count)

  

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

def getClickCount(newUrl):
    #new = re.match('http://news.gzcc.cn/html/2018/xiaoyuanxinwen_(.*).html',newUrl).group(1).split('/')[1]
    newId = re.search('\_(.*).html',newUrl).group(1).split('/')[-1]  #正则表达式获取ID
    res = requests.get('http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80'.format(newId))
    #new = re.findall('\_(.*).html',newUrl)[0].split('/')[1]
    return(int(res.text.split(".html")[-1].lstrip("(')").rstrip("');")))
# count = getClickCount(newUrl)
# print(count)

  

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

def getNewDetail(newUrl):
    detail_res = requests.get(newUrl)
    detail_res.encoding = 'utf-8'
    detail_soup = BeautifulSoup(detail_res.text, 'html.parser')  # 新闻详情页

    title = detail_soup.select('.show-title')[0].text
    info = detail_soup.select('.show-info')[0].text  # 点击进入页面的时间
    exchangetime = datetime.strptime(info.lstrip('发布时间:')[0:19], "%Y-%m-%d %H:%M:%S")  # 字符转换为时间
    if info.find('来源:') > 0:
        source = info[info.find('来源:'):].split()[0].lstrip('来源:')
    else:
        source = 'none'
    content = detail_soup.select('.show-content')[0].text.strip()  # 正文内容
    click = getClickCount(newUrl)  #调用函数获取点击次数
    print(exchangetime, title, source, click)

# res = requests.get("http://news.gzcc.cn/html/xiaoyuanxinwen/")
# res.encoding = 'utf-8'
# soup = BeautifulSoup(res.text,'html.parser')
# for news in soup.select('li'):
#     if len(news.select('.news-list-title'))>0 : #排除为空的li
#         newUrl = news.select('a')[0].attrs['href']
#         getNewDetail(newUrl)
#         break

  

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

def getList(newUrl):
    res = requests.get("http://news.gzcc.cn/html/xiaoyuanxinwen/")
    res.encoding = 'utf-8'
    soup = BeautifulSoup(res.text, 'html.parser')
    for news in soup.select('li'):
        if len(news.select('.news-list-title')) > 0:  # 排除为空的li
            a = news.select('a')[0].attrs['href']
            getNewDetail(a) #调用函数获取链接
            #print(newUrl)
# res = requests.get("http://news.gzcc.cn/html/xiaoyuanxinwen/")
# res.encoding = 'utf-8'
# soup = BeautifulSoup(res.text,'html.parser')
# n = int(soup.select('.a1')[0].text.rstrip('条'))// 10 + 1
#
# for i in range(1,n+1):
#     #print(i)
#     listPageUrl = 'http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i)
#     getList(listPageUrl)
#
# newUrl = 'http://news.gzcc.cn/html/xiaoyuanxinwen/'
# getList(newUrl)

  

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

def getPageH(newUrl):  #新闻列表页的总页数
    res = requests.get("http://news.gzcc.cn/html/xiaoyuanxinwen/")
    res.encoding = 'utf-8'
    soup = BeautifulSoup(res.text,'html.parser')
    n = int(soup.select('.a1')[0].text.rstrip('条'))
    return (n // 10 + 1) #n为总页数,10为一页有10条数据,n对10取余再+1

  

11. 获取全部新闻列表页的全部新闻详情。(以从第n页到n+1页,一页数据为例)

pageUrl = 'http://news.gzcc.cn/html/xiaoyuanxinwen'
n = getPageH(newUrl) #获取页码的总数
#print(n)
for i in range(n,n+1): #第n页到n+1页,一页数据为例,获取最后一页的链表
    listPageUrl = 'http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i)
    getList(listPageUrl)

  

转载于:https://www.cnblogs.com/qq2647409627/p/8785816.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值