使用正则表达式,取得点击次数,函数抽离
1. 用正则表达式判定邮箱是否输入正确。
1 import re 2 r='^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$' 3 e='67890222@qq.com' 4 if re.match(r,e): 5 print(re.match(r,e).group(0)) 6 else: 7 print('非邮箱格式!')

2. 用正则表达式识别出全部电话号码。
1 str='''版权所有:广州商学院 地址:广州市黄埔区九龙大道206号 2 学校办公室:020-82876130 招生电话:020-82872773 3 粤公网安备 44011602000060号 粤ICP备15103669号''' 4 r = '\d{3}-\d{8}|\d{4}-\d{7}' 5 n=re.findall(r,str) 6 for i in n: 7 print(i)

3. 用正则表达式进行英文分词。re.split('',news)
1 str='''Completely destroy information stored without your knowledge or approval: 2 Internet history, Web pages and pictures from sites visited on the Internet, 3 unwanted cookies, chatroom conversations, deleted e-mail messages, temporary files, 4 the Windows swap file, the Recycle Bin, previously deleted files, valuable corporate 5 trade secrets, Business plans, personal files, photos or confidential letters, 6 etc.''' 7 n=re.split(r'[ \t\n]+',str) 8 for i in n: 9 print(i)

4. 使用正则表达式取得新闻编号
1 newurl='http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0404/9183.html' 2 newid=re.search('\_(.*).html',newurl).group(1).split('/')[-1] 3 print(newid)

5. 生成点击次数的Request URL
1 newurl='http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0404/9183.html' 2 newid=re.search('\_(.*).html',newurl).group(1).split('/')[-1] 3 res='http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80'.format(newid) 4 print(res)

6. 获取点击次数
1 newurl='http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0404/9183.html' 2 newid=re.search('\_(.*).html',newurl).group(1).split('/')[-1] 3 res=requests.get('http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80'.format(newid)) 4 r1=int(res.text.split(".html")[-1].lstrip("('").rstrip("');")) 5 print(r1)

7. 将456步骤定义成一个函数 def getClickCount(newsUrl)
1 def getClickCount(newsUrl): 2 newid=re.search('\_(.*).html',newsUrl).group(1).split('/')[-1] 3 res=requests.get('http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80'.format(newid)) 4 return int(res.text.split(".html")[-1].lstrip("('").rstrip("');"))
8. 将获取新闻详情的代码定义成一个函数 def getNewDetail(newsUrl):
def getNewDetail(newsUrl): resd = requests.get(newsUrl) resd.encoding = 'utf-8' soupd = BeautifulSoup(resd.text, 'html.parser') title = soupd.select('.show-title')[0].text info = soupd.select('.show-info')[0].text ti = 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' click = getClickCount(newsUrl) print(ti, title, source, click)
9. 取出一个新闻列表页的全部新闻 包装成函数def getListPage(pageUrl):
1 def getListPage(pageUrl): 2 res = requests.get(pageUrl) 3 res.encoding = 'utf-8' 4 soup = BeautifulSoup(res.text, 'html.parser') 5 for news in soup.select('li'): 6 if len(news.select('.news-list-title')) > 0: 7 g = news.select('a')[0].attrs['href'] 8 print(g) 9 getNewDetail(g)
10. 获取总的新闻篇数,算出新闻总页数包装成函数def getPageN():
1 def getPageN(): 2 res = requests.get('http://news.gzcc.cn/html/xiaoyuanxinwen/') 3 res.encoding = 'utf-8' 4 soup = BeautifulSoup(res.text, 'html.parser') 5 pagenumber=int(soup.select('.a1')[0].text.rstrip('条')) 6 page = pagenumber//10+1 7 return page
11. 获取全部新闻列表页的全部新闻详情。
1 pageUrl='http://news.gzcc.cn/html/xiaoyuanxinwen/' 2 n=getPageN() 3 for i in range(1,n+1): 4 print(i) 5 listPageUrl='http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i) 6 getListPage(listPageUrl)

浙公网安备 33010602011771号