Last active
December 24, 2015 00:49
-
-
Save yanmhlv/6718991 to your computer and use it in GitHub Desktop.
вытаскивание новостей с сайта aionplanet.ws
Возвращает генератор
в генераторе возвращается None, в случае отсутствия блока с новостью, либо кортеж (даты новости, текста "Обновлены серверы Aion Planet|Айон Планет", детали обновления и названия обновления)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
from lxml.html import parse | |
import re | |
def get_news(url = None): | |
if url: | |
doc = parse(url) | |
for news in doc.xpath('//*[@id="content"]/div')[0]: | |
if news.attrib.get('class') == 'news_block': | |
body = news.text_content().strip() | |
server_update, date, details, title = "", "", "", "" | |
match = re.search(ur"\d{2,2}\.\d{2,2}\.\d{4,4}", body, re.U) | |
if match: | |
s0, s1 = match.start(), match.end() | |
date = (body[s0:s1], s0, s1) | |
match = re.search(ur"Обновлены серверы (Aion Planet|Айон Планет)", body, re.U) | |
if match: | |
s0, s1 = match.start(), match.end() | |
server_update = (body[s0:s1], s0, s1) | |
details = [i for i in body[server_update[2]:].strip().split(';')] | |
title = body[date[2]:s0].strip() | |
yield date[0], server_update[0], details, title | |
else: | |
yield None | |
news = list(get_news('http://aionplanet.ws'))[0] # берется крайняя новость | |
print news[0], news[1] # дата и текст "обновлены сервера aion planet|айон планет" | |
print ''.join(news[2]) # список обновлений | |
print news[3] # название обновления |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment