I`m trying to write parser for this site but everytime i get the two same errors:

Viewed 22

here is the code

import csv
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.common


def write_csv(result):
with open('olx.csv', 'w', newline='') as f:
    writer = csv.DictWriter(f, dialect='excel', fieldnames=['name', 'phone', 'region', 'ad_url'])
    writer.writeheader()
    for item in result:
        writer.writerow(item)


url_list = 'https://100realty.ua/realty_search/apartment/sale/cur_3'

result = []
for i in range(1, 2 + 1):
print('Parsing page # ' + str(i) + ' of ' + str(2))
adpageurl = url_list + '?page=' + str(i)
adpage = requests.get(adpageurl)
adpagesoup = BeautifulSoup(adpage.text, 'lxml')
alist = adpagesoup.find_all('a', class_='image-field__link')
for a_element in alist:
    ad_url = 'https://100realty.ua' + a_element.get('href')
    adresponse = requests.get(ad_url, allow_redirects=True)
    adpage = BeautifulSoup(adresponse.text, 'lxml')
    driver.find_element(By.CLASS_NAME, "object-contacts-one-phone").click()
    phone = adpage.find('a', class_='object-contacts-phone-link')
    item = {'phone': phone.text, 'ad_url': ad_url, }
    result.append(item)

 write_csv(result)

errors: this one is when i use driver = webdriver.common

line 31, in <module>
driver.find_element(By.CLASS_NAME, "object-contacts-one-phone").click()
AttributeError: module 'selenium.webdriver.common' has no attribute 'find_element'

this one is when i use driver = webdriver.Firefox

Traceback (most recent call last):
 File "C:\Users\romav\OneDrive\Рабочий стол\pythonProject\obyavlenia.py", line 31, in <module>
   driver.find_element(By.CLASS_NAME, "object-contacts-one-phone").click()
 File "C:\Users\romav\OneDrive\Рабочий стол\pythonProject\venv\lib\site- 
  packages\selenium\webdriver\remote\webdriver.py", line 855, in find_element
   return self.execute(Command.FIND_ELEMENT, {
AttributeError: 'str' object has no attribute 'execute'

How can i fix it. `Cause i goggled so much but still couldn't find any answers

1 Answers

@Роман Войтковский, replace the following line in your code:

driver = webdriver.common

with the following:

driver = webdriver.Firefox(executable_path="../../resources/geckodriver.exe")

For this, you will need to download the geckodriver from this link Gecko Drivers based on your OS, place it in a folder in your project, and change the path on the line above to that location.

Related