【发布时间】:2021-09-29 10:07:57
【问题描述】:
我正在尝试从this site 的父 id='search-properties' 中抓取子元素 href 属性中的链接。我首先尝试使用 find_elements_by_id 定位元素,然后使用 find_elements_by_css_selector 定位链接,但我不断得到 AttributeError: 'list' object has no attribute 'find_elements_by_css_selectors' 这样做时我尝试使用 find_elements_by_tag_name 以及 find_elements_by_xpath 而是抓取链接它实际上抓取了对我没有用的链接内的细节。所以在环顾四周后,我终于找到了这段代码
from logging import exception
from typing import Text
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
import time
import pandas as pd
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import csv
from selenium import webdriver
PATH = "C:/ProgramData/Anaconda3/scripts/chromedriver.exe" #always keeps chromedriver.exe inside scripts to save hours of debugging
driver =webdriver.Chrome(PATH) #preety important part
driver.get("https://www.gharbazar.com/property/search/?_q=&_vt=1&_r=0&_pt=residential&_si=0&_srt=latest")
driver.implicitly_wait(10)
house=driver.find_elements_by_tag_name("a")
# traverse list
for lnk in house:
# get_attribute() to get all href
print(lnk.get_attribute('href'))
这段代码的问题是它会刮掉所有的链接,这意味着它还有一些绝对不必要的链接,就像这张图片don't need javascript void 一样。 最后,对于分页,我尝试遵循这个answer,但得到了无限循环,所以我不得不删除分页代码。总之,我正在尝试获取具有 id = 'search-properties' 的多个页面的链接
【问题讨论】:
-
网站上的右键单击被禁用了吗?
-
是的,但是你可以通过按 ctrl+ shift+ j 来访问它
-
好的!是的,我这样做是为了获取元素
-
请专注于从下一页抓取链接
标签: python selenium web-scraping pagination parent-child