1. 用正则表达式判定邮箱是否输入正确。
import re email = '736583887@qq.com' if re.match('^[0-9a-zA-Z_]{0,19}@[0-9a-zA-Z]{1,13}\.[com,cn,net]{1,3}$', email): print("true") else: print("false")
2. 用正则表达式识别出全部电话号码。
import re str = '''版权所有:广州商学院 地址:广州市黄埔区九龙大道206号 学校办公室:020-82876130 招生电话:020-82872773''' tel = re.findall('\d[0-9]{0,2}-\d[0-9]{6,8}', str) for i in tel: print(i)
3. 用正则表达式进行英文分词。re.split('',news)
import re english='''Every single time you access a website, you leave tracks. Tracks that others can access. If you don't like the idea, find out what software can help you cover them. ''' word = re.split('[\s,.?\+]',english) for i in word: print(i)
4. 使用正则表达式取得新闻编号
newsUrl = ' http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0401/9163.html' newsId = re.search('\_(.*).html', newsUrl).group(1).split('/')[-1] print(newsId)
5. 生成点击次数的Request URL
import re newsUrl = ' http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0401/9163.html' newsId = re.search('\_(.*).html', newsUrl).group(1).split('/')[-1] url = ('http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80'.format(newsId)) print(url)
6. 获取点击次数
import requests import re newsUrl = ' http://news.gzcc.cn/html/2018/xiaoyuanxinwen_0401/9163.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)) count = int(res.text.split('.html')[-1].lstrip("(')").rstrip("');")) print(count)
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)) count = int(res.text.split('.html')[-1].lstrip("(')").rstrip("');")) return count
8. 将获取新闻详情的代码定义成一个函数 def getNewDetail(newsUrl):
def getNewDetail(newsUrl): res = requests.get(newsUrl) res.encoding = 'utf-8' soup = BeautifulSoup(res.text, 'html.parser') title = soup.select(".show-title")[0].text content = soup.select("#content")[0].text info = soup.select(".show-info")[0].text dt = datetime.strptime(info.lstrip('发布时间:')[0:19], '%Y-%m-%d %H:%M:%S') if info.find('作者:')>0: author = info[info.find('作者:'):].split()[0].lstrip('作者:') else: author = 'none' if info.find('审核:')>0: auditing = info[info.find('审核:'):].split()[0].lstrip('审核:') else: auditing = 'none' if info.find('来源:')>0: source = info[info.find('来源:'):].split()[0].lstrip('来源:') else: source = 'none' if info.find('摄影:') > 0: photo = info[info.find('来源:'):].split()[0].lstrip('摄影:') else: photo = 'none' print(title,content) print(dt.strftime('%Y/%m/%d'), author, auditing, source,photo)
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:
newsUrl = news.select('a')[0].attrs['href'] # URL
getNewDetail(newsUrl)
10. 获取总的新闻篇数,算出新闻总页数包装成函数def getPageN():
def getPageN(): url = 'http://news.gzcc.cn/html/xiaoyuanxinwen/' res = requests.get(url) res.encoding = 'utf-8' soup = BeautifulSoup(res.text,'html.parser') n =int(soup.select('.a1')[0].text.rstrip('条'))// 10 + 1 return n
11. 获取全部新闻列表页的全部新闻详情。
newsUrl='http://news.gzcc.cn/html/xiaoyuanxinwen/' n=getPageN() for i in range(2,n+1): print(i) pageUrl='http://news.gzcc.cn/html/xiaoyuanxinwen/{}.html'.format(i) getListPage(pageUrl)