【问题标题】:Python Crawler: deal with "load more" buttonPython Crawler:处理“加载更多”按钮
【发布时间】:2017-09-27 23:26:57
【问题描述】:

我正在学习python爬虫,我想知道如何处理位于以下网址中的“加载更多”按钮:

https://www.photo.net/search/#//Sort-View-Count/All-Categories/All-Time/Page-1

(我试图爬取所有图片)

我的当前代码正在使用 beautifulsoup:

from urllib.request import *

from http.cookiejar import CookieJar

from bs4 import BeautifulSoup

url = 'https://www.photo.net/search/#//Sort-View-Count/All-Categories/All- Time/Page-1'

cj = CookieJar()

opener = build_opener(HTTPCookieProcessor(cj))

try:
    p = opener.open(url)

    soup = BeautifulSoup(p, 'html.parser')

except Exception as e:

    print(str(e))

【问题讨论】:

  • 您是否尝试增加 URL 末尾的页码?您可以尝试循环浏览一些页面并抓取其中的内容。
  • 是的,我试过了,但除非你点击那个按钮,否则它无法加载。

标签: python web-crawler


【解决方案1】:

好吧,我有一个解决方案。

您应该尝试用于 python 的 Selenium 模块。

1) 下载 Chrome 驱动程序

2) 通过 pip 安装 Selenium

这是一个如何使用它的示例

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

browser = webdriver.Chrome('Path to chrome driver')
browser.get()
while True:
    button = WebDriverWait(browser,10).until(EC.presence_of_element_located((By.LINK_TEXT, 'Load More')))
    button.click()

【讨论】:

    猜你喜欢
    • 2021-04-20
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多