Bạn có chắc chắn muốn xóa bài viết này không ?
Bạn có chắc chắn muốn xóa bình luận này không ?
VnExpress Toilet Paper - Pet Project
Requirement
Hôm bữa đọc được bài https://kipalog.com/posts/Hoc-va-cai-thien-kien-thuc-tu-cac-du-an-ca-nhan-pet-project thấy hay quá. Tính cũng định viết con app để lúc đi vệ sinh có cái mà đọc.
Mục tiêu thì cũng như bạn ấy đề cập: Web nhẹ, nhỏ, đẹp, xinh .
Frontend: None - Chỉ lấy html và render.
Backend: Python.
Server provider: Heroku
Source control: Github
Breakdown
Nguồn bài từ đầu: Rss của VnExpress - https://vnexpress.net/rss
Xử lí Rss bằng gì: Feedparser
Scraping detail bài viết: BeautifulSoup
App: Flask
Home
<!--Danh sách rss của VnExpress-->
<li><button name = "rss" value="/rss/thoi-su.rss" type="submit">Thời sự</button></li>
<li><button name = "rss" value="/rss/kinh-doanh.rss" type="submit">Kinh doanh</button></li>
<li><button name = "rss" value="/rss/giai-tri.rss" type="submit">Giải trí</button></li>
<li><button name = "rss" value="/rss/the-thao.rss" type="submit">Thể thao</button></li>
<li><button name = "rss" value="/rss/phap-luat.rss" type="submit">Pháp luật</button></li>
<li><button name = "rss" value="/rss/giao-duc.rss" type="submit">Giáo dục</button></li>
<li><button name = "rss" value="/rss/phap-luat.rss" type="submit">Pháp luật</button></li>
<li><button name = "rss" value="/rss/the-thao.rss" type="submit">Thể thao</button></li>
<li><button name = "rss" value="/rss/gia-dinh.rss" type="submit">Đời sống</button></li>
<li><button name = "rss" value="/rss/du-lich.rss" type="submit">Du lịch</button></li>
<li><button name = "rss" value="/rss/khoa-hoc.rss" type="submit">Khoa học</button></li>
<li><button name = "rss" value="/rss/so-hoa.rss" type="submit">Số hóa</button></li>
<li><button name = "rss" value="/rss/tam-su.rss" type="submit">Tâm sự</button></li>
<li><button name = "rss" value="/rss/cuoi.rss" type="submit">Cười</button></li>
Scrap entry from rss
python
rss = request.form['rss']
NewsFeed = feedparser.parse("https://vnexpress.net" + rss)
return render_template('result.html',NewsFeed = NewsFeed,year=year)
html
<ul>
{% for entry in NewsFeed.entries %}
<li><button name = "link" value="{{ entry.link }}">{{ entry.title }}</button></li>
{% endfor %}
</ul>
Detail bài viết
python
def detail():
link = request.form['link']
soup = BeautifulSoup(requests.get(link).content,'html.parser')
title = soup.find(class_="title_news_detail mb10").text
paragraphs = soup.find("article")
return """<h1>"""+title+"""</h1>""" + str(paragraphs) #Lẽ ra nên scrap sạch sẽ hơn nhưng mình lười quá =)).
Thành quả
app: https://vnexpresstoilet.herokuapp.com
github: https://github.com/PhiHuyHoang/ToiletPaper
Chúc các bạn đi vệ sinh vui vẻ ...






