以下是代码
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'
我怎么才能把它修好。因为我睁了那么多眼睛,但还是找不到任何答案
2条答案
按热度按时间nfs0ujit1#
@РоманВойтковский,请在您的代码中替换以下行:
具有以下功能:
为此,您需要根据您的操作系统从Gecko Drivers链接下载gecko驱动程序,将其放置在项目的文件夹中,并将上面行上的路径更改为该位置。
eni9jsuy2#
我这样解决了我的问题: