selenium 我试图为这个站点编写解析器,但每次我都得到两个相同的错误:

xzv2uavs  于 2022-11-10  发布在  其他
关注(0)|答案(2)|浏览(151)

以下是代码

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)

错误:这是在我使用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'

这是当我使用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'

我怎么才能把它修好。因为我睁了那么多眼睛,但还是找不到任何答案

nfs0ujit

nfs0ujit1#

@РоманВойтковский,请在您的代码中替换以下行:

driver = webdriver.common

具有以下功能:

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

为此,您需要根据您的操作系统从Gecko Drivers链接下载gecko驱动程序,将其放置在项目的文件夹中,并将上面行上的路径更改为该位置。

eni9jsuy

eni9jsuy2#

我这样解决了我的问题:

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)

相关问题