Reputation: 121
I want to iterate rows from SQLite table and the code is working, however it runs the function 4 times before moving to the next row.
def get_ship_tm_window():
# connect to database
conn = sqlite3.connect('PSC.sdb')
# create cursor
c = conn.cursor()
f = conn.cursor()
c.execute("DELETE FROM 'inspections tm'")
conn.commit()
with requests.Session() as s:
for row in c.execute("SELECT * FROM ships ORDER BY ISM"):
print(row)
ship_imo = row[1]
print(ship_imo)
get_tm_windows_access(s, ship_imo)
f.execute(
"INSERT INTO 'inspections tm' VALUES(:name, :imoship, :lastinsp, :ship_risk, :date_pii , :date_pi, :prio)",
{
'name': row[0],
'imoship': row[1],
'lastinsp': '',
'ship_risk': get_tm_windows_access(s, ship_imo)[0],
'date_pii': get_tm_windows_access(s, ship_imo)[2],
'date_pi': get_tm_windows_access(s, ship_imo)[3],
'prio': get_tm_windows_access(s, ship_imo)[1]
})
conn.commit()
conn.close()
get_ship_tm_window()
The result is:
('HANNA OLDENDORFF', 9731614, 'Anglo-Eastern (Germany) GmbH', 5365996, 'High', 'High') # prints row OK
9731614 #prints imo OK
7B9E9C8F-EAF8-46FA-A09E-6B852A5C7F4C #1ST TIME
High Risk Ship #1ST TIME
Priority I #1ST TIME
05/09/2019 #1ST TIME
05/11/2019 #1ST TIME
7B9E9C8F-EAF8-46FA-A09E-6B852A5C7F4C #2nd time
High Risk Ship #2nd time
Priority I #2nd time
05/09/2019 #2nd time
05/11/2019 #2nd time
7B9E9C8F-EAF8-46FA-A09E-6B852A5C7F4C #3RD TIME
High Risk Ship #3RD TIME
Priority I #3RD TIME
05/09/2019 #3RD TIME
05/11/2019 #3RD TIME
7B9E9C8F-EAF8-46FA-A09E-6B852A5C7F4C #4th time
High Risk Ship #4th time
Priority I #4th time
05/09/2019 #4th time
05/11/2019 #4th time
Here is should stop, but I get it 3 more times until it prints the new row. After the 4th time, it moves to the next row.
EDITED.
def get_tm_windows_access(s, ship_imo):
date_from = datetime.now() - relativedelta(months=36)
date_to = datetime.now()
header = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.',
'Cookies': 'PHPSESSID=xxxxxxxxxxxxxxxxxxxxxxxxxxxx'}
s.headers.update(header)
r = s.get('https://apcis.tmou.org/public/')
str_number = re.findall("<span[^>]+(.*?)</span>", r.text)[0]
numbers = re.findall('[0-9]+', str_number)
captcha = int(numbers[0]) + int(numbers[1])
payload = {'captcha': captcha}
r = s.post('https://apcis.tmou.org/public/?action=login', data=payload)
check_text = re.findall('<b>(.*?)</b>', r.text)[0]
payload1 = {'Param': 0, 'Value': ship_imo, 'imo': ship_imo, 'callsign': '', 'name': '', 'compimo': '',
'compname': '', 'From': date_from.strftime('%d.%m.%Y'), 'Till': date_to.strftime('%d.%m.%Y'),
'authority': 0, 'flag': 0, 'class': 0, 'ro': 0, 'type': 0, 'result': 0, 'insptype': -1, 'sort1': 0,
'sort2': 'DESC', 'sort3': 0,
'sort4': 'DESC'
}
r = s.post('https://apcis.tmou.org/public/?action=getships', data=payload1)
try:
acces_code = re.findall('<input type="hidden" value="([^>]*)"/>', r.text)[0]
print(acces_code)
payload2 = {'HUID': acces_code}
r = s.post('https://apcis.tmou.org/public/?action=getship', data=payload2)
try:
ship_risk = re.findall('<h2>(.*?), (.*?)</h2>', r.text)[0][0]
ship_prio = re.findall('<h2>(.*?), (.*?)</h2>', r.text)[0][1]
ship_window_raw = re.findall('<h2 class="winInspRange">Window Inspection Range:\n\n(.*?)</h2>', r.text)[0]
ship_window_numbers = re.findall('[0-9]+', ship_window_raw)
pII = ship_window_numbers[0] + '/' + ship_window_numbers[1] + '/' + ship_window_numbers[2]
pI = ship_window_numbers[3] + '/' + ship_window_numbers[4] + '/' + ship_window_numbers[5]
return ship_risk, ship_prio, pII, pI
except IndexError:
ship_risk = re.findall('<h2>(.*?), (.*?)</h2>', r.text)[0][0]
ship_prio = re.findall('<h2>(.*?), (.*?)</h2>', r.text)[0][1]
pII = ''
pI = ''
return ship_risk, ship_prio, pII, pI
except IndexError:
return '', '', '', ''
Here you have the remaining of the code. It is very strange for me that it runs 4 times.
Upvotes: 1
Views: 83
Reputation: 107652
Avoid multiple calls to your other function, get_tm_windows_access()
, by assigning result to a named variable then index this tuple for query parameters. Currently, you do not assign the first call to anything, so it runs with printed lines as you show but its returned output is not saved anywhere. Simply have that output saved to a variable.
def get_ship_tm_window():
# connect to database
conn = sqlite3.connect('PSC.sdb')
# create cursor
c = conn.cursor()
f = conn.cursor()
c.execute("DELETE FROM 'inspections tm'")
conn.commit()
with requests.Session() as s:
for row in c.execute("SELECT * FROM ships ORDER BY ISM"):
print(row)
ship_imo = row[1]
print(ship_imo)
access_data = get_tm_windows_access(s, ship_imo) # ADJUSTED LINE
f.execute(
"INSERT INTO 'inspections tm' VALUES(:name, :imoship, :lastinsp, :ship_risk, :date_pii , :date_pi, :prio)",
{
'name': row[0],
'imoship': row[1],
'lastinsp': '',
'ship_risk': access_data[0], # ADJUSTED LINE
'date_pii': access_data[2], # ADJUSTED LINE
'date_pi': access_data[3], # ADJUSTED LINE
'prio': access_data[1] # ADJUSTED LINE
})
conn.commit()
conn.close()
Upvotes: 1