当前位置: 首页 > 知识库问答 >
问题:

硒中的蟒蛇/BeautifulSoup

全丰
2023-03-14

我正在尝试使用本教程从一个使用selenium and beautiful soup的站点提取房地产列表信息:https://medium.com/@ben.sturm/scraping-house-listing-data-using-Selenium-and-Beautiful Soup-1CBB94BA9492

目的是在找到“下一页”按钮之前收集第一页的所有href链接,导航到下一页并收集该页上的所有链接等等。

尝试用一个单一的函数来实现这一点,并在每一页重复,但不能弄清楚它为什么不工作。对于学习代码来说是新的,而且似乎太琐碎了,还找不到一个答案。如有任何帮助,我将不胜感激

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
import sys
import numpy as np
import pandas as pd
import regex as re


driver = webdriver.Chrome
url = "http://property.shw.co.uk/searchproperties/Level2-0/Level1-0-181-236-167-165/Units/Development-or-House-and-Flat-or-Investment-or-Land-or-Office-or-Other/UnitIds-0/For-Sale"
driver.get(url)
try:
    wait = WebDriverWait(driver, 3)
    wait.until(EC.presence_of_element_located((By.ID, "body1")))
    print("Page is Ready!")
except TimeoutException:
    print("page took too long to load")


def get_house_links(url, driver, pages=3):
    house_links = []
    driver.get(url)
    for i in range(pages):
        soup = BeautifulSoup(driver.page_source, 'html.parser')
        listings = soup.find_all("a", class_="L")
        page_data = [row['href'] for row in listings]
        house_links.append(page_data)
        time.sleep(np.random.lognormal(0, 1))
        next_button = soup.find_all("a", class_="pageingBlock darkBorder")
        next_button_link = ['http://property.shw.co.uk'+row['href'] for row in next_button]
        if i < 3:
            driver.get(next_button_link[0])
    return house_links
get_house_links(url, driver)

共有1个答案

洪弘壮
2023-03-14

class_=“PageingBlock DarkBorder”也与上一页按钮匹配,因此NEXT_BUTTON_LINK[0]会将您发送回上一页。你需要更精确的定位器

next_button = soup.select('img[src*="propNext"]')
if next_button:
    next_button = next_button[0].find_parent('a')
    next_button_link = 'http://property.shw.co.uk' + next_button['href']
    driver.get(next_button_link)
 类似资料:
  • 我正在使用sublime来编写python脚本,下面的代码是为python中的硒使用webdriver_manager包自动安装驱动程序 代码运行良好,但我得到了类似的警告 如何修复这样的错误?

  • 我试图选择和控制www.ziprecruiter.com网站上的下拉菜单使用硒称为半径。因为我是初学者,我似乎不明白为什么我不能控制这个下拉半径菜单。我尝试使用查找ID,名称,Xpath和选择,但似乎没有工作。我想在半径下拉菜单中选择选项“25英里”,有什么想法吗?

  • 我正在尝试点击图中所示的下拉菜单 这就是我尝试过的 但它给了我这个错误 NoSuchElementException: Message:找不到element://div[@ class = ' choosed-container choosed-container-single ']//a[@ class = ' choosed-single choosed-single-with-deselec

  • 我正在运行Ubuntu 18.04。 我使用mysql连接器-python连接Python到MySQL。 我使用的是Python 3.6.7,并且已经安装了mysql连接器-python。 我已经安装了mysql连接器-python-py3_8.0.13-1ubuntu18.10_all.deb. 在运行Python脚本时,mysql。连接器模块似乎加载正确,但脚本在碰到光标时失败。next()具

  • 假设我有一些资源,我想在用python编写的aws lambda中的不同请求之间共享。我应该如何实现这一点? 是否有“启动后”挂钩,或者我应该在第一次调用时惰性地创建资源?“延迟初始化”的缺点是,它意味着一些请求会随机变慢,因为您选择了一个消费者来承担启动成本。 此外…这些资源会在lambda可执行文件被“冻结”后幸存下来吗? 本页https://docs.aws.amazon.com/lambd

  • 问题内容: 请告诉我,我做错了什么?我尝试拖放Selenium,但是每次遇到错误“ AttributeError:move_to需要WebElement”时, 这是我的代码: 我也尝试过,像这样: 总是出现“ AttributeError:move_to需要WebElement” 问题答案: 返回的列表,(和其他方法)接受单个。采用