I get an error:
AttributeError: 'Park_Stat_Thread' object has no attribute 'park_stat
when I run the script below. Any idea how to pass back the park_stat variable?
As you can see I use multi-threading to crawl a list of URLs from the web:
class Park_Stat_Thread(Thread):
def __init__(self, thread_num):
super().__init__()
self.thread_num = thread_num
def run(self):
print('starting thread %s.' % self.thread_num)
process_queue(self)
print('exiting thread %s.' % self.thread_num)
def process_queue(self):
while True:
try:
x = my_queue.get(block=False)
except queue.Empty:
return
else:
parking_stat_getter(self,x)
time.sleep(1)
def parking_stat_getter(self,x):
base = 'http://www.ahuzot.co.il/Parking/ParkingDetails/?ID={}'
response = requests.get(base.format(self.thread_num))
soup = BeautifulSoup(response.text, "lxml")
try:
self.park_stat = 0
for img in soup.find_all('img' , attrs={'src': re.compile("ParkingIcons")}):
soup_img = str(img)
if 'male' in soup_img:
self.park_stat=1
elif 'meat' in soup_img:
self.park_stat=2
elif 'panui' in soup_img:
self.park_stat=3
except AttributeError:
self.park_stat = np.nan
return self.park_stat
def get_parkings_Stat(parking_id_lists):
threads = [Park_Stat_Thread(t) for t in range(1, 8)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
park_details = dict(zip(parking_id_lists, [thread.park_stat for thread in threads]))
return park_details