-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvnexpress scraper basic.py
More file actions
38 lines (34 loc) · 948 Bytes
/
vnexpress scraper basic.py
File metadata and controls
38 lines (34 loc) · 948 Bytes
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
# -*- coding: utf-8 -*-
"""
Created on Tue May 12 00:56:04 2020
@author: PC
"""
from bs4 import BeautifulSoup
from urllib.error import URLError
from urllib.error import HTTPError
from urllib.request import urlopen
import time
def scraping_vnexpress():
try:
html = urlopen('https://vnexpress.net/').read()
except URLError as e:
print('URL Error!')
except HTTPError as e:
print(e)
else:
print('Succesfully read!')
time.sleep(3)
bsObject = BeautifulSoup(html, 'html.parser')
tagname = ['h1','h2','h3','h4','h5']
titles = []
for tag in tagname:
if tag != tagname[0]:
titles += bsObject.find_all(tag, {'class':'title-news'})
else:
titles = bsObject.find_all(tag, {'class':'title-news'})
n = 0
for title in titles:
print(title.text)
n += 1
print(n)
scraping_vnexpress()