Answer the question
In order to leave comments, you need to log in
One is not added to the variable a second time during the loop, how to solve?
I'm parsing data from 2GIS, pagination doesn't work.
Works in other functions.
Here is the code
def bar(self):
kafe = []
bar = []
pab = []
rest = []
num = []
name = []
other = []
url = 'https://2gis.ru/kaluga/search/бар'
self.driver.get(url)
time.sleep(5)
number = self.driver.find_element_by_xpath('//span[@class="searchBar__mediaTabTextValue searchBar__mediaTabTotal"]').text
time.sleep(3)
name1 = self.driver.find_elements_by_xpath('//h3[@class="mediaMiniCard__name"]')
for t in name1:
title = t.get_attribute('title')
name.append(title)
name2haslogo = self.driver.find_elements_by_xpath('//h3[@class="mediaMiniCard__name _hasLogo"]')
for p in name2haslogo:
tit = p.get_attribute('title')
name.append(tit)
nameBarPOD = self.driver.find_elements_by_xpath('//h4[@class="mediaMiniCard__description"]')
for po in nameBarPOD:
if('ресторан' in str(po.text)):
rest.append(1)
if('паб' in str(po.text)):
pab.append(1);
if('кафе' in str(po.text)):
pab.append(1)
if('бар' in str(po.text)):
bar.append(1)
else:
other.append(1)
intut = self.driver.find_elements_by_xpath('//a[@class="mediaMiniCard__link"]')
okrlen = []
okroct = []
okrmosk = []
for i in intut:
time.sleep(5)
i.click()
time.sleep(5)
okrug = self.driver.find_element_by_xpath('//div[@class="mediaAddress__drilldown"]').text
Slen = okrug.find("Ленинский")
Soct = okrug.find("Октябрьский")
Smos = okrug.find("Московский")
if Slen != -1:
okrlen.append(1);
if Soct != -1:
okroct.append(1)
if Smos != -1:
okrmosk.append(1)
exi = self.driver.find_element_by_link_text('Назад')
ActionChains(self.driver).move_to_element(exi).click().perform()
time.sleep(3)
### 2 - конца, все значения записываются в массив(список)
page_count12 = 1
time.sleep(5)
while True:
page_count12 += 1;
try:
s = self.driver.find_element_by_link_text(str(page_count12))
if s.get_attribute('class') == 'pagination__page':
s.click()
time.sleep(2)
name1 = self.driver.find_elements_by_xpath('//h3[@class="mediaMiniCard__name"]')
for t in name1:
tot = t.get_attribute('title')
name.append(tot)
name2 = self.driver.find_elements_by_xpath('//h3[@class="mediaMiniCard__name _hasLogo"]')
for t1 in name2:
tetatet = t1.get_attribute('title')
name.append(tetatet)
nameBarPOD = self.driver.find_elements_by_xpath('//h4[@class="mediaMiniCard__description"]')
for po in nameBarPOD:
if('ресторан' in str(po.text)):
rest.append(1)
if('паб' in str(po.text)):
pab.append(1);
if('кафе' in str(po.text)):
pab.append(1)
if('бар' in str(po.text)):
bar.append(1)
else:
other.append(1)
intut = self.driver.find_elements_by_xpath('//a[@class="mediaMiniCard__link"]')
for i in intut:
i.click()
time.sleep(3)
okrug = self.driver.find_element_by_xpath('//div[@class="mediaAddress__drilldown"]').text
Slen = okrug.find("Ленинский")
Soct = okrug.find("Октябрьский")
Smos = okrug.find("Московский")
if Slen != -1:
okrlen.append(1);
if Soct != -1:
okroct.append(1)
if Smos != -1:
okrmosk.append(1)
exi = self.driver.find_element_by_link_text('Назад')
ActionChains(self.driver).move_to_element(exi).click().perform()
time.sleep(2)
except NoSuchElementException:
break
sum1 = len(pab)
print("Баров в Ленинском округе " + str(len(okrlen)))
print("Баров в Октябрьском округе " + str(len(okroct)))
print("Баров в Московском округе " + str(len(okrmosk)))
if(sum1 > 0):
print('Количество пабов')
print(sum1)
sum2 = len(rest)
if(sum2 > 0):
print('Количество ресторанов-баров')
print(sum2)
sum3 = len(bar)
if(sum3 > 0):
print('Количество баров в городе ' + str(sum3))
sum4 = len(other)
if(sum4 > 0):
print('Другого вида ' + str(sum4))
sum5 = len(kafe)
if(sum5 > 0):
print('Количество кафешек в городе ' + str(sum))
print("Всего " + str(number))
print("Проверка")
# подготовка массива к отправке к MySql
# убираем повторяющие значения в массиве, оптимизируем
# добавляем данные в базу и каждую неделю берем значения и если разница более чем 1 то добавляем в определенное поле значение 1
new_nameforSql = []
for i in name:
if i not in new_nameforSql:
new_nameforSql.append(i)
if((len(new_nameforSql) - int(number)) > 3):
print("Пройдена")
else:
print("Не пройдена")
print(number)
print(new_nameforSql)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question