2019-04-26 20:53:04 +00:00
|
|
|
import urllib.request,urllib.parse,urllib.error
|
|
|
|
|
from lxml import html
|
|
|
|
|
import requests
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Site:
|
2019-05-02 18:04:10 +00:00
|
|
|
siteName = ""
|
|
|
|
|
baseURL = ""
|
|
|
|
|
searchURLString = ""
|
|
|
|
|
xPath = dict()
|
|
|
|
|
xPath["searchArticle"] = ""
|
|
|
|
|
xPath["searchLinks"] = ""
|
|
|
|
|
xPath["newsArticle"] = ""
|
|
|
|
|
xPath["readHeadlineTitle"] = ""
|
|
|
|
|
xPath["readHeadlineText"] = ""
|
|
|
|
|
xPath["readArticleText"] = ""
|
|
|
|
|
|
2019-04-26 20:53:04 +00:00
|
|
|
header_values = {
|
|
|
|
|
'Connection:' : 'Keep-alive',
|
|
|
|
|
'name' : 'Michael Foord',
|
|
|
|
|
'location' : 'Northampton',
|
|
|
|
|
'language' : 'German',
|
|
|
|
|
'User-Agent': 'Mozilla 4/0'}
|
2019-05-02 18:04:10 +00:00
|
|
|
|
2019-04-26 20:53:04 +00:00
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def search_article(self, topic):
|
2019-05-02 18:04:10 +00:00
|
|
|
searchURL = self.searchURLString + topic.replace(" ", "+")
|
2019-04-26 20:53:04 +00:00
|
|
|
site = requests.get(searchURL)
|
|
|
|
|
tree = html.fromstring(site.content)
|
|
|
|
|
|
2019-05-02 18:04:10 +00:00
|
|
|
articles = tree.xpath(self.xPath["searchArticle"])
|
|
|
|
|
links = tree.xpath(self.xPath["searchLinks"])
|
2019-04-26 20:53:04 +00:00
|
|
|
return articles, links
|
|
|
|
|
|
|
|
|
|
def get_news(self):
|
2019-05-02 18:04:10 +00:00
|
|
|
searchURL = self.baseURL
|
2019-04-26 20:53:04 +00:00
|
|
|
site = requests.get(searchURL)
|
|
|
|
|
tree = html.fromstring(site.content)
|
|
|
|
|
|
2019-05-02 18:04:10 +00:00
|
|
|
articles = tree.xpath(self.xPath["newsArticle"])
|
2019-04-26 20:53:04 +00:00
|
|
|
return articles
|
|
|
|
|
|
|
|
|
|
def read_headlines(self, url):
|
|
|
|
|
site = requests.get(url)
|
|
|
|
|
tree = html.fromstring(site.content)
|
|
|
|
|
|
2019-05-02 18:04:10 +00:00
|
|
|
title = tree.xpath(self.xPath["readHeadlineTitle"] )
|
|
|
|
|
title += tree.xpath(self.xPath["readHeadlineText"])
|
2019-04-26 20:53:04 +00:00
|
|
|
return title
|
|
|
|
|
|
|
|
|
|
def read_article(self, url):
|
|
|
|
|
site = requests.get(url)
|
|
|
|
|
tree = html.fromstring(site.content)
|
|
|
|
|
|
|
|
|
|
title = self.read_headlines(url)
|
2019-05-02 18:04:10 +00:00
|
|
|
title += tree.xpath(self.xPath["readArticleText"])
|
2019-04-26 20:53:04 +00:00
|
|
|
return title
|
2019-05-02 18:04:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class Golem(Site):
|
|
|
|
|
siteName = "golem"
|
|
|
|
|
baseURL = "https://www.golem.de/"
|
|
|
|
|
searchURLString = "https://suche.golem.de/search.php?l=10&q="
|
|
|
|
|
Site.xPath["searchArticle"] = '//span[@class="dh2 head2"]/text()'
|
|
|
|
|
Site.xPath["searchLinks"] = '//ol[@class="list-articles"]/li/header//@href'
|
|
|
|
|
Site.xPath["newsArticle"] = '//h2[@class="head2"]/text()'
|
|
|
|
|
Site.xPath["readHeadlineTitle"] = '//header/h1/span[@class="dh1 head5"]/text()'
|
|
|
|
|
Site.xPath["readHeadlineText"] = '//header/p/text()'
|
|
|
|
|
Site.xPath["readArticleText"] = '//div[@class="formatted"]/p/text()'
|
|
|
|
|
|