-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
43 lines (36 loc) · 1.36 KB
/
script.py
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
39
40
41
42
43
##!/usr/bin/env python
# -*- coding: utf-8 -*-
#Autor: Álvaro Ordóñez
#Instruccións: Completar as liñas que están comentados.
from bs4 import BeautifulSoup
import requests,tweepy
ficheiro = open('/home/???/escru.txt', 'r+') #Onde se garda o valor do anterior escrutinio
porcentaxe=ficheiro.readline().strip()
url="http://resultadoslocales2015.interior.es/?????????????????????" #Web de Interior co concello que queremos publicar
auth = tweepy.OAuthHandler("API Key", "API Secret") #API Keys de Twitter
auth.set_access_token("Token", "Token-Secret") #Tokens de Twitter
api = tweepy.API(auth)
tweet=""
r = requests.get(url)
data = r.text
soup = BeautifulSoup(data)
escrutado=soup.find("div", {"id": "xescrutado"})
for span in escrutado.find_all('span', recursive=False):
porcentaxeNova=span.string.strip()
tweet=tweet+"Escrutado " + porcentaxeNova + "\n"
if(porcentaxe!=porcentaxeNova):
porcentaxe=porcentaxeNova;
ficheiro.seek(0); ficheiro.write(porcentaxeNova);
table=soup.find(id="TVRESULTADOS")
table_body = table.find('tbody')
rows = table_body.find_all('tr')
for row in rows:
partido=row.findAll('th')[0].string.strip()
cols=row.findAll('td')
concelleiros=cols[3].string.strip()
if(concelleiros!=""):
tweet=tweet+partido + " " + concelleiros + "\n"
tweet=tweet+"#SantiagoDeCompostela"
print tweet
api.update_status(status=tweet)
ficheiro.close()