web2py add row in form table - row

I'm building an edifact translator/editor. For displaying an existing translation I have the following controller:
import shutil
import sys
import fileinput
def edit_datafile():
#inlees gedeelte van een bestaand datatype
file = open('/home/wouter/DESADVCB.pcf', 'r')
datas = []
data = {}
row = {}
j = 1
for line in file:
meg = line.strip().split(':', 3)
if line.startswith('#'):
c = 1
unmark = line.strip().split('#')
datas.append('#')
datas.append(unmark[1])
data[j,c] = datas
datas=[]
j = j+1
else:
if '.' in meg[0]:
t = meg[0].split('.')
if '+' in t[0]:
a = t[0].split('+')
datas.append(a[0])
datas.append(a[1])
else:
datas.append(t[0])
datas.append(t[1])
try:
t[2]
datas.append(t[2])
except IndexError, e:
datas.append('')
else:
if '+' in meg[0]:
a = meg[0].split('+')
datas.append(a[0])
datas.append(a[1])
else:
datas.append(meg[0])
datas.append('')
datas.append('')
datas.append('')
datas.append(meg[1])
datas.append(meg[2])
try:
meg[3]
if ':' in line:
reg = meg[3].strip().split(':')
c = 1
for teg in reg:
seg = teg.split('=')
if '.' in seg[0]:
t = seg[0].split('.')
datas.append(t[0])
datas.append(t[1])
try:
t[2]
datas.append(t[2])
except IndexError, a:
datas.append('')
try:
t[3]
datas.append(t[3])
except IndexError, a:
datas.append('')
s= seg[1].split('.', 3)
datas.append(s[0])
datas.append(s[1])
datas.append(s[2])
datas.append(s[3])
data[j,c]=datas
c = c+1
else:
datas.append(seg[0])
datas.append('')
datas.append('')
datas.append('')
s= seg[1].split('.', 3)
datas.append(s[0])
datas.append(s[1])
datas.append(s[2])
datas.append(s[3])
data[j,c]=datas
c= c+1
datas=[]
j = j+1
else:
seg = meg[3].strip().split('=')
if '.' in seg[0]:
t = seg[0].split('.')
datas.append(t[0])
datas.append(t[1])
try:
t[2]
datas.append(t[2])
except IndexError, a:
datas.append('')
try:
t[3]
datas.append(t[3])
except IndexError, a:
datas.append('')
else:
datas.append(seg[0])
datas.append('')
datas.append('')
datas.append('')
s= seg[1].split('.', 3)
datas.append(s[0])
datas.append(s[1])
datas.append(s[2])
datas.append(s[3])
data[j] = datas
datas=[]
j = j+1
except IndexError, a:
c = 1
datas.append('')
datas.append('')
datas.append('')
datas.append('')
datas.append('')
datas.append('')
datas.append('')
datas.append('')
data[j,c] = datas
datas=[]
j = j+1
# form om regels toe te voegen
form_ar = FORM(
TABLE(THEAD(
TR(
TH('Nieuw:'),
TH('Toevoegen:'),
TH('Nummer:')
)),
TBODY(TR(
TD(SELECT('rij','remark', value='rij',_name='t')),
TD(SELECT('voor', 'na', value='na',_name='w')),
TD(SELECT([(key,'.',skey) for (key,skey) in sorted(data.iterkeys())],_name='p')),
TD(INPUT(_type="submit", _name="make", _value="Maak"))
))))
form_er = FORM(
TABLE(
THEAD(TR(
TH('Line'),
TH('Name'),
TH('Qualifier'),
TH('Field'),
TH('Subfield'),
TH('Match'),
TH('Set'),
TH('Field'),
TH('Subfield'),
TH('Position'),
TH('Length'),
TH('Position'),
TH('Lenght'),
TH('Type'),
TH('Name')
)),
TBODY([TR(
TD(key,'.',skey),
TD(INPUT(_name='N',_value=data[key,skey][0],_style='width:30px')),
TD(INPUT(_name='Q',_value=data[key,skey][1]), _colspan=13,_style='width:450px'), rowid=data[key,skey]
)
if data[key,skey][0]=='#' else
TR(
TD(key,'.',skey),
TD(INPUT(_name='N',_value=data[key,skey][0], _style='width:30px')),
TD(INPUT(_name='Q',_value=data[key,skey][1], _style='width:30px')),
TD(INPUT(_name='IF',_value=data[key,skey][2], _style='width:30px')),
TD(INPUT(_name='IS',_value=data[key,skey][3], _style='width:30px')),
TD(INPUT(_name='IM',_value=data[key,skey][4], _style='width:30px')),
TD(INPUT(_name='IT',_value=data[key,skey][5], _style='width:30px')),
TD(INPUT(_name='OF',_value=data[key,skey][6], _style='width:30px')),
TD(INPUT(_name='OS',_value=data[key,skey][7], _style='width:30px')),
TD(INPUT(_name='IP',_value=data[key,skey][8], _style='width:30px')),
TD(INPUT(_name='IL',_value=data[key,skey][9], _style='width:30px')),
TD(INPUT(_name='OP',_value=data[key,skey][10], _style='width:30px')),
TD(INPUT(_name='OL',_value=data[key,skey][11], _style='width:30px')),
TD(INPUT(_name='OT',_value=data[key,skey][12], _style='width:10px')),
TD(INPUT(_name='ON',_value=data[key,skey][13])), rowid=data[key,skey]
)
if skey==1 else
TR(
TD(key,'.',skey),
TD(_maxlength='3',_size='3'),
TD(_colspan=5),
TD(INPUT(_name='OF',_value=data[key,skey][0], _style='width:30px')),
TD(INPUT(_name='OS',_value=data[key,skey][1], _style='width:30px')),
TD(INPUT(_name='IP',_value=data[key,skey][2], _style='width:30px')),
TD(INPUT(_name='IL',_value=data[key,skey][3], _style='width:30px')),
TD(INPUT(_name='OP',_value=data[key,skey][4], _style='width:30px')),
TD(INPUT(_name='OL',_value=data[key,skey][5], _style='width:30px')),
TD(INPUT(_name='OT',_value=data[key,skey][6], _style='width:10px')),
TD(INPUT(_name='ON',_value=data[key,skey][7])), rowid=data[key,skey]
) for (key,skey) in sorted(data.iterkeys())]),_id="example",_class="display"),
INPUT(_type="submit", _name="save", _value="Save Page")
)
return dict(data=data,form_ar=form_ar, form_er=form_er, f=f)
the file is like this:
BGM+351::1:2=1.8.0.doorlopend nummer CobanaFruchtring
#remark test
DTM+191:::1.3=316.8.D.aankomstdatum (verwacht)
NAD+SU:::2=46.8.0.Lieferantennummer:4.1=54.9..Lieferantenname
NAD+BY:::2=63.8.0.ontvangernummer:4.1=71.9..ontvangernaam
TDT+20:::8.4=299.17..bootnaam
CPS::2
LIN::3
PIA+5:::3.1=80.34..Hoofdsoort/Varieteit:4.1=216.17..EAN-code:5.1=182.17..Verpakking
PIA+1:::2.1=114.17..Marke:3.1=29.17..Ursprungland ISO-code:4.1=250.8..Size:5.1=148.17..Qualitaetsbezeichnung
MEA+AAA.2.1:::3.2=423.8.3.nettogewicht per collo
MEA+AAB.2.1:::3.2=439.8.3.brutogewicht per collo
MEA+AAJ.2.1:::3.2=258.4.0.vpp
QTY+21:::1.2=455.8.0.verwachte colli
FTX+PAL:::4.1=182.17..Palettenart
RFF+ON:::1.2.1.7=233.6.0.Partie-Nummer:1.2.8.3=239.3..Partie-pos
RRF+ZZZ:::1.2.1.8=471.8.2.SBX partijnummer:1.2.16.4=479.4..SBX varieteitscode:1.2.20.4=483.4..SBX merkcode:1.2.28.4=487.4..SBX categoriecode:1.2.32.4=490.1..SBX verpakkingscode
LOC+18:::2.4=324.17..Ankunfthafen
PCI+IEN:::2=262.11..Containernummer
I want to be able to add rows in the form without a submit on a desired position. I have looked at powertable, datatables and jQuery to accomplish this but I cannot seem to find a suitable solution.
Can someone help me?

If I understand correctly, you need to add row dynamicly to the table in any position.
If so, you can use jQuery method before() or after().
For example, to insert row before selected row:
$('#table tbody tr').live('click',function(event) {
$(this).before("<tr><td>data1</td></tr>")
});

Related

"Unboundlocalerror: Local Variable "Val" Referenced before Assignment" Error

I have been trying to get my script to loop in such a way that it will load the outputs in 1 file, and then when it's done loading everything move the values to output file 2 , erase the values in output file 1 and start reloading them, then when those are down move the values into output two (overwriting the old ones) repeat.
I have been pretty successful so far and don't know what else to add to my script and am hoping someone here knows why I keep getting ""Unboundlocalerror: Local Variable "Val" Referenced before Assignment" error randomly midway through the loading process, when I have a very small input file, the script performs how I want.
Does anyone know how I can change my script to fix that error, I have tried to understand why it is happening but cannot.
I have tried to research it thoroughly but none of the suggestions I have found have worked (or I implemented them incorrectly, I have attached my script. Thanks!
import urllib2,re,urllib,urlparse,csv,sys,time,threading,codecs,shutil
from bs4 import BeautifulSoup
def extract(url):
try:
sys.stdout.write('0')
# global file
page = urllib2.urlopen(url).read()
soup = BeautifulSoup(page, 'html.parser')
product = soup.find("div", {"class": "js-product-price"})
price = product.findNext('div',{'class': 'js-price-display'}).getText().strip()
oos = product.findNext('p', attrs={'class': "price-oos"})
if oos is None:
oos = 'In Stock'
else:
oos = oos.getText()
val = url + "," + price + "," + oos + "," + time.ctime() + '\n'
# ifile.write(val)
sys.stdout.write('1')
except Exception as e:
print e
return val
while True:
ifile = open('output.csv', "w", 0)
inputs = csv.reader(open('input.csv'))
# inputs = csv.reader(codecs.open('input.csv', 'rU', 'utf-16'))
ifile.write('URL' + "," + 'Price' + "," + 'Stock' + "," + "Time" + '\n')
for i in inputs:
ifile.write(extract(i[0]))
ifile.close()
Update:
Thanks for the help guys! This is my new script:
import urllib2,re,urllib,urlparse,csv,sys,time,threading,codecs,shutil
from bs4 import BeautifulSoup
def extract(url):
try:
sys.stdout.write('0')
# global file
page = urllib2.urlopen(url).read()
soup = BeautifulSoup(page, 'html.parser')
product = soup.find("div", {"class": "js-product-price"})
price = product.findNext('div',{'class': 'js-price-display'}).getText().strip()
oos = product.findNext('p', attrs={'class': "price-oos"})
if oos is None:
oos = 'In Stock'
else:
oos = oos.getText()
val = url + "," + price + "," + oos + "," + time.ctime() + '\n'
# ifile.write(val)
sys.stdout.write('1')
except Exception as e:
print e
else:
return val
while True:
ifile = open('output.csv', "w", 0)
inputs = csv.reader(open('input.csv'))
# inputs = csv.reader(codecs.open('input.csv', 'rU', 'utf-16'))
ifile.write('URL' + "," + 'Price' + "," + 'Stock' + "," + "Time" + '\n')
for i in inputs:
val_to_write = extract(i[0])
if val_to_write:
ifile.write(val_to_write)
ifile.close()
shutil.copy('output.csv', 'output2.csv')
print("finished")
With the above script I am now getting the error: "ValueError: I/O operation on closed file". Thanks
Use try-except-else as you would only want to return val if no exception was raised (if an exception was raised then val wouldn't be assigned to when you try to return it). Another suggestion is not to use a "catch-em-all" except block.
def extract(url):
try:
sys.stdout.write('0')
# global file
page = urllib2.urlopen(url).read()
soup = BeautifulSoup(page, 'html.parser')
product = soup.find("div", {"class": "js-product-price"})
price = product.findNext('div',{'class': 'js-price-display'}).getText().strip()
oos = product.findNext('p', attrs={'class': "price-oos"})
if oos is None:
oos = 'In Stock'
else:
oos = oos.getText()
val = url + "," + price + "," + oos + "," + time.ctime() + '\n'
# ifile.write(val)
sys.stdout.write('1')
except Exception as e:
print e
else:
return val
But be warned: if an exception does occur then extract will return None and the calling code will have to take account for that, for example:
for i in inputs:
val_to_write = extract(i[0])
if val_to_write:
ifile.write(val_to_write)
ifile.close()

Arduino Python Multiple Ultrasonic sensors upload to MySQL

import os
from time import *
import time
import threading
import serial
import datetime
import sys
import MySQLdb as mdb
ser = serial.Serial("/dev/ttyUSB0", 9600)
distances = ser.readline()
distances.splitlines
distances=distances.splitlines()
try:
con = mdb.connect('130.127.199.212', 'root', 'Fr24clemson', 'autonomous');
cur = con.cursor()
except:
print "Error opening serial port."
sys.exit(1)
resp = ""
droptable="DROP TABLE autonomous.ultrasonic_sensor_data"
cur.execute(droptable)
createtable="CREATE TABLE autonomous.ultrasonic_sensor_data (n_FL DOUBLE NULL ,w_FR DOUBLE NULL,n_RL DOUBLE NULL ,w_RR DOUBLE NULL,t_unixtime DOUBLE NULL,t_time TIMESTAMP)"
cur.execute(createtable)
try:
while True:
for d in distances :
d=d.split(' ')
print d
FL=float(d[0])
FR=float(d[1])
RL=float(d[2])
RR=float(d[3])
unixtime=datetime.datetime.now().strftime("%s")
sql = "insert into ultrasonic_sensor_data(n_FL, w_FR, n_RL, w_RR, t_unixtime) values(%s, %s, %s, %s, %s)" % (FL,FR,RL,RR, unixtime)
print sql
cur.execute(sql)
print "Rows inserted: %s" % cur.rowcount
con.commit()
time.sleep(1)#set to whatever
resp = ""
except:
print sys.exc_info()[0]
finally:
if con:
con.close()
time.sleep(1) #set to whatever
My current code. Problem is the same value of the sensor is now getting struck the values inserted are not the updated ones.
This is the output
insert into ultrasonic_sensor_data(n_FL, w_FR, n_RL, w_RR, t_unixtime) values(33.0, 12.0, 133.0, 37.0, 1468345652)
Rows inserted: 1
['33', '12', '133', '37']
insert into ultrasonic_sensor_data(n_FL, w_FR, n_RL, w_RR, t_unixtime) values(33.0, 12.0, 133.0, 37.0, 1468345653)
Rows inserted: 1
['33', '12', '133', '37']
insert into ultrasonic_sensor_data(n_FL, w_FR, n_RL, w_RR, t_unixtime) values(33.0, 12.0, 133.0, 37.0, 1468345654)
Rows inserted: 1
You can do this to get the values out of that string into a list:
l = ['39 15 12 18']
to_insert = map(lambda x: int(x),l[0].split())
Then insert with:
conn = MySQLdb.connect('your connection details')
cur = conn.cursor()
cur.execute('insert into your_table (s1, s2, s3, s4) values (%s,%s,%s,%s);',to_insert)

PYTHON: Searching two files for common lines and collecting their contents into set

I have a task to compare two comma separated files. If the first two columns exist in both files, then I have to collect the remaining columns into set from both the files in my results.
If I have the following two files:
a.txt
1,2,3,4
2,4,7,5
3,8,6,7
4,9,5,6
3,8,7,2
b.txt
1,2,4,6
2,3,6,5
3,8,9,2
4,9,6,9
3,5,2,3
6,2,7,3
I want to get the results:
1,2(3,4,4,6)
3,8(6,7,7,2,9,2)
4,9(5,6,6,9)
Is there a more efficient way to implement it? especially as the files maybe large and not fit in the available memory of my computer.
The following is my implement.
KEYNOTFOUND = '<KEYNOTFOUND>'
class dict_cls(object):
#staticmethod
def dict_diff(first, second):
diff = {}
for key in first.keys():
if (not second.has_key(key)):
diff[key] = (first[key], KEYNOTFOUND)
elif (first[key] != second[key]):
diff[key] = (first[key], second[key])
for key in second.keys():
if (not first.has_key(key)):
diff[key] = (KEYNOTFOUND, second[key])
return diff
if __name__ == '__main__':
dict1 = {(1,2):(3,4),(2,4):(7,5),(3,8):(6,7),(4,9):(5,6),(3,8):(7,2)}
dict2 = {(1,2):(4,6),(2,3):(6,5),(3,8):(9,2),(4,9):(6,9),(3,5):(2,3),(6,2):(7,3)}
print dict_cls.dict_diff(dict1, dict2)
import datetime
class FindCommKey(object):
def __init__(self):
self.combine = {}
self.counter = {}
self.result = {}
def find_common_key(self, target_file):
with open(target_file, 'r+') as file_handler:
for line in file_handler:
print(line, end='')
__line = list(map(int, line.strip().split(',')))
key, value = tuple(__line[:2]), __line[2:]
if key in self.combine:
self.combine[key] = self.combine[key] + value
else:
self.combine[key] = value
if key in self.counter:
self.counter[key] = self.counter[key] + 1
else:
self.counter[key] = 1
for k1, v1 in self.counter.items():
if v1 >= 2:
self.result[k1] = self.combine[k1]
print()
return self.result
if __name__ == '__main__':
files = ['ds1.txt', 'ds2.txt']
print("Started at: {}{}".format(datetime.datetime.now(), '\n'))
print('Initial data:')
fck = FindCommKey()
for f in files:
fck.find_common_key(f)
print("Write to dic finished at: {}{}".format(datetime.datetime.now(), '\n'))
print('Result set:')
for k, v in fck.result.items():
print(','.join(map(str, k)), tuple(v))
print("{}Finished at: {}".format('\n', datetime.datetime.now()))

Raspberry PI - pinging multiple IPs

Would either like the code analysed and fixed or point me in the right direction please. So many errors, some overcome, others not.
Program runs on a Raspberry PI2 and should try and ping specific IP addresses and return a result.
Very new to programming, you can probably tell! Not sure if I need a ping library or can do it without
import sys
import time
from pushbullet import Pushbullet
import serial
class Users(object):
def __init__(self, name=None, ip=None):
self.name = name
self.ip = ip
self.status = 'out'
pb = Pushbullet("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") #Pushbullet ID removed
userList = []
userList.append(Users("Ali", "192.18.1.14"))
userList.append(Users("Sophie", "192.18.1.9"))
userList.append(Users("TV", "192.18.1.7"))
try:
while True:
print "Checking... " + time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime())
for user in userList:
result = os.system ("ping -n 1 " = user.ip)
oldStatus = user.status
if (result == 0):
#What we'll do if a device is detected
if (oldStatus == 'out'):
push = pb.push_note("Home Pi", user.name + " is home")
user.status = 'in'
print user.name + " is home"
else:
#What we'll do if a device is NOT not detected
if (oldStatus == 'in'):
push = pb.push_note("Home Pi", user.name + " has just left")
user.status = 'out'
print user.name + " is out"
print "Next check will be in 30 seconds"
time.sleep(30)
except (KeyboardInterrupt, SystemExit):
I modified your code to work, i don't have pushbullet. Compare my new code with your previous to see the difference and the errors
import sys
import time
#from pushbullet import Pushbullet
#import serial
#you need import os
import os
class Users(object):
def __init__(self, name=None, ip=None):
self.name = name
self.ip = ip
self.status = 'out'
#pb = Pushbullet("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") #Pushbullet ID removed
userList = []
userList.append(Users("Notebook", "192.168.1.2"))
userList.append(Users("TV", "192.168.1.4"))
try:
while True:
print "Checking... " + time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime())
for user in userList:
#result = os.system ("ping -n 1 " = user.ip)
# to concatenate string you need + sign
#result = os.system("ping -n 1 " + user.ip)
# -n is a wrong option for ubuntu
result = os.system("ping -c 1 " + user.ip)
oldStatus = user.status
if (result == 0):
#What we'll do if a device is detected
if (oldStatus == 'out'):
#push = pb.push_note("Home Pi", user.name + " is home")
user.status = 'in'
print user.name + " is home"
else:
#What we'll do if a device is NOT not detected
if (oldStatus == 'in'):
#push = pb.push_note("Home Pi", user.name + " has just left")
user.status = 'out'
print user.name + " is out"
print "Next check will be in 30 seconds"
time.sleep(30)
#wrong identation
except (KeyboardInterrupt, SystemExit):
sys.exit(0)

How to prevent Selenium from opening a pop-up to save the file?

I'm scraping the site Quicker.com but every time getting an error on random pages.
The error is:
UnexpectedAlertPresentException: Alert Text: C:\Users\HEYPIL~1\AppData\Local\Temp\Pkwnr4IA.php.part could not be saved, because the source file could not be read.
Try again later, or contact the server administrator.
<super: <class 'WebDriverException'>, <UnexpectedAlertPresentException object>>
My code:
from selenium import webdriver
import csv
import re
import hashlib
from selenium.common.exceptions import UnexpectedAlertPresentException
from selenium.common.exceptions import WebDriverException
import socket
import time
import datetime
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
date = st.encode('utf8')
IPAdd = socket.gethostbyname(socket.gethostname())
counter = 5
initial = []
base = "http://mumbai.quikr.com/Individual/0-50000000/Houses-Apartments-for-Sale/w1072?imageAds=Y&l=You_are-Price"
string = "&page="
while(counter == 5 or counter < 40):
base2 = base+string+str(counter)
if (counter < 39):
initial.append(base2)
elif(counter == 40):
initial.append(base)
else:
base2 = base
counter += 1
for n in initial:
result = []
driver = webdriver.Firefox()
driver.get(n)
main_page = '//a[#class="adttllnk unbold"]'
for a in driver.find_elements_by_xpath(main_page):
l = a.get_attribute('href')
result.append(l)
print len(result)
driver.close()
for url in result:
try:
driver = webdriver.Firefox()
driver.get(url)
items = []
desc_path = '//div[#id="ad_description"]'
img_path = '//div[#class="bigImg_wapp"]//img[#src]'
prop = '//div[#itemprop="name"]//h1[#class="ad_title translate"]'
amenities = '//div[#class="ad-atrbt-panel"]//span[#class="ad-atrbt-val"]//span[#class="attribVal newattribVal"]'
phone = '//span[#class="NoVerified-Text"]'
for x1 in driver.find_elements_by_xpath(img_path):
img = (x1.get_attribute('src')).encode('utf8')
print '\n'+img
for x2 in driver.find_elements_by_xpath(desc_path):
desc = (x2.text).encode('utf8')
print '\n'+ desc
for x3 in driver.find_elements_by_xpath(prop):
prop_title = (x3.text).encode('utf8')
print '\n'+prop_title
for x4 in driver.find_elements_by_xpath(amenities):
value = (x4.text).encode('utf8')
items.append(value)
print '\n'
print items
locality = items[0]
locality1 = locality.encode('utf8')
a = (locality1 if (isinstance(locality1,int) == False) else "")
bhk = items[1]
bhk1 = bhk.encode('utf8')
if(bhk1 == "4+ BHK"):
b = "4"
else:
bhk2 = [int(z) for z in bhk1.split() if z.isdigit()]
b = ((str(bhk2).strip('[')).strip(']')).strip()
furnish = items[2]
if(isinstance(furnish,int) == False ):
furnish1 = furnish.encode('utf8')
if((furnish1 == "Semi-Furnished") or (furnish1 == "Unfurnished") or (furnish1 == "Fully Furnished") or (furnish1 == "Unfurnished,Unf...")):
c = furnish1
else:
d = furnish1
elif(isinstance(furnish,int) == True):
furnish1 = furnish.encode('utf8')
d = furnish1
else:
c = ""
sqft = items[3]
if(isinstance(sqft,int)==True):
sqft1 = [int(xyz) for xyz in sqft.split() if xyz.isdigit()]
sqft2 = ((str(sqft1).strip('[')).strip(']')).strip()
d = sqft2.encode('utf8')
elif(isinstance(sqft,int)==False):
sqft1 = sqft.encode('utf8')
if((sqft1 == "Semi-Furnished") or (sqft1 == "Unfurnished") or (sqft1 == "Fully Furnished") or (sqft1 == "Unfurnished,Unf...")):
c = sqft1
else:
d = sqft1
else:
d = ""
atz = '\t'
print a,atz,b,atz,c,atz,d
for x5 in driver.find_elements_by_xpath(phone):
biz = (((x5.text).lstrip('+91')).strip()).encode('utf8')
if(len(biz)== 9):
biz_phone = '9'+biz
elif(len(biz) < 7 and len(biz) > 4):
biz_phone = '080'+biz
elif(len(biz) > 9 or len(biz) < 12):
biz_phone = biz
elif(len(biz) == 4 or len(biz) < 4):
biz_phone = biz.strip(biz)
else:
print '\nInvalid Business_phone'
print '\n'+biz_phone
driver.close()
hash_key = hashlib.md5("marketing#"+biz_phone+".com"+"Individual"+prop_title).hexdigest()
unique_key = ('I_'+hash_key).encode('utf8')
except (NameError, IndexError, WebDriverException, UnexpectedAlertPresentException) as e:
print "Failed to open: "+url
driver.close()
fieldname = ['Date','URL']
with open("C:\Users\Heypillow\Desktop\scrapWork\properties\\Failed_to_open_url.csv",'a') as h:
write = csv.DictWriter(h,fieldnames=fieldname,lineterminator = '\n')
write.writerow({'Date':date,
'URL':url})
I've blocked the pop-up in Firefox() but yet a pop-up is coming which addressing me to save a .php file and raises this exception.
I've already used that exception in the "except" part yet it's interrupting the code to work further and it's getting stopped just after this exception rises.
So, every time this exception rises, I have to restart the program. Thus I would like to download all the data by running the code through out the night,which is impossible with this circumstances…
How can I prevent this pop-up from opening?
(If I would have been able to upload a screenshot of the pop-up,it would have been easier to understand it.)