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

Time:09-29

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

CodePudding user response:

@Роман Войтковский, 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.

CodePudding user response:

@ketanvj thank you very much. But i solved my problem like this:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager

path = ChromeDriverManager().install()
driver = webdriver.Chrome(service=ChromeService(path))

driver.get("https://domik.ua/uk/kupiti-kvartiru-kiyiv")
driver.find_element(By.CLASS_NAME, "viewTile__link viewTile__link_fill").click()
driver.find_element(By.CLASS_NAME, "hideTelephone__text_instead").click()
print(driver.find_element(By.CLASS_NAME, "hideTelephone__text_tel").text)
get_url = driver.current_url
print(get_url)
  • Related