기술 블로그

정규표현식 간단 연습 본문

Python

정규표현식 간단 연습

parkit 2019. 10. 26. 22:21
728x90
반응형


lambda 람다 활용






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import requests
from bs4 import BeautifulSoup
# 위의 두 모듈이 없는 경우에는 pip install requests bs4 실행
 
def get_news_content(url):
    response = requests.get(url)
    content = response.text
 
    soup = BeautifulSoup(content, 'html5lib')
 
    div = soup.find('div', attrs = {'id' : 'harmonyContainer'})
    
    content = ''
    for paragraph in div.find_all('p'):
        content += paragraph.get_text()
        
    return content
 
news1 = get_news_content('https://news.v.daum.net/v/20190617073049838')
print(news1)
 
import re
 
email_reg = re.compile(r'[\w-]+@[\w.]+\w+')
email_reg.search(news1)
 
webs = ['http://www.test.co.kr'
        'https://www.test1.com'
        'http://www.test.com'
        'ftp://www.test.com'
        'http:://www.test.com',
       'htp://www.test.com',
       'http://www.google.com'
       'https://www.homepage.com.']
 
web_reg = re.compile('https?://[\w.]+\w+$')
 
list(map(lambda w:web_reg.search(w) != None, webs))
cs




















728x90
반응형

'Python' 카테고리의 다른 글

lambda  (0) 2019.10.26
정규표현식  (0) 2019.10.26
class 연산자 재정의  (0) 2019.10.26
class 상속  (0) 2019.10.26
class staticmethod  (0) 2019.10.26