学会使用正则表达式
1. 用正则表达式判定邮箱是否输入正确。
import rew='^(/w)+(\.\w+)*@(\w)+((\/\w{2,3}){1,3})$'s='915908192@qq.com'if re.match(w,s): print(re.match(w,s).group(0))else: print('error')
2. 用正则表达式识别出全部电话号码。
import restr ='''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 renews = ''''It was summer. A duck was sitting in her nest. Her little ducklings were about to hatched. One egg after another began to crack,but the biggest one was still there. At last, it cracked. The baby was big and ugly. '''word = re.split('[\s,.?]',news)print(word)
4. 使用正则表达式取得新闻编号
import renewsUrl = 'http://news.gzcc.cn/html/2017/xiaoyuanxinwen_095/8249.html'www=re.search('\_(.*).html',newsUrl).group(1)print(www)
5. 生成点击次数的Request URL
import renewsUrl = 'http://news.gzcc.cn/html/2017/xiaoyuanxinwen_095/8249.html'newsId = re.search('\_(.*).html', newsUrl).group(1).split('/')[-1]qw = 'http://oa.gzcc.cn/api.php?op=count&id=8249&modelid=80'.format(newsId)print(qw)
6. 获取点击次数
import requestsimport renewsUrl = 'http://news.gzcc.cn/html/2017/xiaoyuanxinwen_095/8249.html'newsId=re.search('\_(.*).html', newsUrl).group(1).split('/')[-1]pp = requests.get('http://oa.gzcc.cn/api.php?op=count&id=8249&modelid=80'.format(newsId))ClickCount=(int(pp.text.split('.html')[-1].lstrip("(')").rstrip("');")))print(ClickCount)
7. 将456步骤定义成一个函数 def getClickCount(newsUrl):
def getClickCount(newsUrl): newId = re.search('\_(.*).html', newsUrl).group(1).split('/')[-1] res="http://oa.gzcc.cn/api.php?op=count&id={}&modelid=80".format(newId) return(int(res.text.split('.html')[-1].lstrip("(')").rstrip("');")))
8. 将获取新闻详情的代码定义成一个函数 def getNewDetail(newsUrl):
def getNewsDetail(newsUrl): resd = requests.get(newsUrl) resd.encoding = 'utf-8' soupd = BeautifulSoup(resd.text, 'html.parser') # 打开新闻详情页 c = soupd.select('.show-title')[0].text # 正文 info = soupd.select('.show-info')[0].text ws = 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 = soupd.select('.show-content')[0].text.strip() click = getClickCount(newsUrl) print(ws, c, newsUrl, source, click)
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'] # 链接 getNewsDetail(newsUrl)
10. 获取总的新闻篇数,算出新闻总页数包装成函数def getPageN():
def getPageN(): res = requests.get('http://news.gzcc.cn/html/xiaoyuanxinwen/') res.encoding = 'utf-8' soup = BeautifulSoup(res.text, 'html.parser') pagenumber=int(soup.select('.a1')[0].text.rstrip('条')) page = int(soup.select('.a1')[0].text.rstrip('条'))//10+1 return page
11. 获取全部新闻列表页的全部新闻详情。
m=getPageN()for i in range(1,m+1): pageUrl = 'http://news.gzcc.cn/html/xiaoyuanxinwen/' getListPage(pageUrl)