This is my first program in Python. Following code is taken from different posts (mostly this site) and combined together so that I can automate my routine task.
It's working properly but I need opinion from experts to enhance it further.
import mechanize
from bs4 import BeautifulSoup
import numpy as np
import csv
import os
# reading file
np.genfromtxt('EAN.txt', delimiter ='\n')
dtype=str
with open('EAN.txt') as f:
lines = f.read().splitlines()
new_dictionary = {}
count = 0
for line in lines:
count += 1
new_dictionary['sequence_{}'.format(count)] = line
# searching items
print "Running..."
for i in new_dictionary:
myean = new_dictionary[i]
url = "https://mysite"
br = mechanize.Browser()
br.set_handle_robots(False)
br.open(url)
br.select_form(id="searchForm")
br["q"] = myean
res = br.submit()
content = res.read()
with open("mechanize_results.html", "a") as f:
f.write(content)
soup = BeautifulSoup(open("mechanize_results.html"),'html.parser')
for div in soup.findAll('div', {'class': 'small-12 columns product-title'}):
a = div.findAll('a')[1]
#writing file
if a is None:
with open('Results.csv', 'ab') as csvfile:
spamwriter = csv.writer(csvfile, delimiter='|',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
spamwriter.writerow([myean, "Rejected"])
#, title.string])
else:
#print myean, '|', a.text.strip()
with open('Results.csv', 'ab') as csvfile:
spamwriter = csv.writer(csvfile, delimiter='|',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
spamwriter.writerow([myean, "Approved", a.text.strip()])
#, title.string])
#deleting file
os.remove("mechanize_results.html")
a=None
i =+ 1
print "%d items Searched" %count
new_dictionary.clear()
raw_input("Press any key to continue...")
Although this code is running perfectly but I was wondering that when I used pyinstaller to create exe file, size became around 400+MB. Is it because, I am importing complete package of 'mechanize', 'numpy', 'csv' and 'os'?