1. 用正則表達式斷定郵箱是否輸入正確。php
import re r= '^(\w)+(\.\w)*@(\w)+((\.\w{2,3}){1,3})$' e=input("請輸入你的郵箱:"); if re.match(r,e): print("您的郵箱輸入正確.") else: print("您的郵箱輸入有誤.")
2. 用正則表達式識別出所有電話號碼。html
import re str='''020-82876130 版權全部:廣州商學院 地址:廣州市黃埔區九龍大道206號 學校辦公室:020-82876130 招生電話:020-82872773 校外:0724-4263864 粵公網安備 44011602000060號 粵ICP備15103669號''' print(re.findall('(\d{3,4})-(\d{6,8})',str))
3. 用正則表達式進行英文分詞。re.split('',news)python
import re string = "Youth is not a time of life; it is a state of mind; it is not a matter of rosy cheeks, red lips and supple knees; it is a matter of the will, a quality of the imagination, a vigor of the emotions; it is the freshness of the deep springs of life. " print(re.split('[\s,.;"?\-]+',string))
4. 使用正則表達式取得新聞編號正則表達式
import re newUrl='http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0404/9183.html' newId = re.findall('\_(.*).html', newUrl)[0].split('/')[1]; print(newId)
5. 生成點擊次數的Request URLspring
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. 獲取點擊次數api
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):url
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):spa
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():code
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);