Python Selenium无法通过链接。Pastebin爬行

拒绝服务

您好,我正在尝试提取给定的10个页面中的所有链接以进行搜索ssh

加载JavaScript之后,我可以从第一页提取前10个链接,然后,我可以单击一次,单击第一页,然后提取接下来的10个链接,但是,当尝试转到第三页时,我得到了一个错误。

这是我的代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import requests
import re

links = []
driver = webdriver.Firefox()
driver.get("http://pastebin.com/search?q=ssh")

# wait for the search results to be loaded
wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".gsc-result-info")))
for link in driver.find_elements_by_xpath("//div[@class='gs-title']/a[@class='gs-title']"):
        if link.get_attribute("href") != None:
            print link.get_attribute("href")
# get all search results links
for page in driver.find_elements_by_xpath("//div[@class='gsc-cursor-page']"):
    driver.implicitly_wait(10) # seconds
    page.click()

    for link in driver.find_elements_by_xpath("//div[@class='gs-title']/a[@class='gs-title']"):
        if link.get_attribute("href") != None:
            print link.get_attribute("href")

这就是我所能获得的,以及我所犯的错误:

python pastebinselenium.py 
http://pastebin.com/u/ssh
http://pastebin.com/gsQWBEZP
http://pastebin.com/gfA12TWk
http://pastebin.com/udWMWdPR
http://pastebin.com/J55238CB
http://pastebin.com/DN2aHvRr
http://pastebin.com/f0rh66kU
http://pastebin.com/3zvY3DSm
http://pastebin.com/fqHVJGEm
http://pastebin.com/3aB7h0fm
http://pastebin.com/3uBAxXu3
http://pastebin.com/cxjRqeSh
http://pastebin.com/5nJPNr3Q
http://pastebin.com/qV0rPNfP
http://pastebin.com/zubt2Yc7
http://pastebin.com/jFrjWYpE
http://pastebin.com/DU7yqjQ1
http://pastebin.com/AFtWHmtE
http://pastebin.com/UVP5behK
http://pastebin.com/hP7XTyv1
Traceback (most recent call last):
  File "pastebinselenium.py", line 21, in <module>
    page.click()
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py", line 74, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webelement.py", line 457, in _execute
    return self._parent.execute(command, params)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 233, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: Element not found in the cache - perhaps the page has changed since it was looked up
Stacktrace:
    at fxdriver.cache.getElementAt (resource://fxdriver/modules/web-element-cache.js:9454)
    at Utils.getElementAt (file:///tmp/tmpzhZSEC/extensions/[email protected]/components/command-processor.js:9039)
    at fxdriver.preconditions.visible (file:///tmp/tmpzhZSEC/extensions/[email protected]/components/command-processor.js:10090)
    at DelayedCommand.prototype.checkPreconditions_ (file:///tmp/tmpzhZSEC/extensions/[email protected]/components/command-processor.js:12644)
    at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpzhZSEC/extensions/[email protected]/components/command-processor.js:12661)
    at fxdriver.Timer.prototype.setTimeout/<.notify (file:///tmp/tmpzhZSEC/extensions/[email protected]/components/command-processor.js:625)

我想从10个页面中提取10个链接(共100个),我只能提取20 =(

我也尝试过这个:

wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".gsc-cursor-box")))

就在之前click,但没有成功。

ec

这个想法是在循环中单击分页链接,等待下一个页码在途中成为活动的收集链接。执行:

from pprint import pprint

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


driver = webdriver.Firefox()
driver.get("http://pastebin.com/search?q=ssh")

# wait for the search results to be loaded
wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".gsc-result-info")))

links = [link.get_attribute("href") for link in driver.find_elements_by_css_selector(".gsc-results .gs-result > .gsc-thumbnail-inside > .gs-title > a.gs-title")]
for page_number in range(2, 11):
    driver.find_element_by_xpath("//div[@class='gsc-cursor-page' and . = '%d']" % page_number).click()

    wait.until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@class, 'gsc-cursor-current-page') and . = '%d']" % page_number)))

    links.extend([link.get_attribute("href") for link in driver.find_elements_by_css_selector(".gsc-results .gs-result > .gsc-thumbnail-inside > .gs-title > a.gs-title")])

print(len(links))
pprint(links)

印刷:

100
['http://pastebin.com/u/ssh',
 'http://pastebin.com/gsQWBEZP',
  ...
 'http://pastebin.com/vtBgrndi',
 'http://pastebin.com/WgXrebLq',
 'http://pastebin.com/Nxui56Gh',
 'http://pastebin.com/Qef0LZPR',
 'http://pastebin.com/yNUh1fRe',
 'http://pastebin.com/2j0d8FzL',
 'http://pastebin.com/g92A2jAq']

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Python爬行Pastebin(JavaScript呈现的网页)

来自分类Dev

无法单击带有Python Selenium的链接

来自分类Dev

Selenium:无法通过XPATH Python查找元素

来自分类Dev

如何使用Selenium Python通过链接文本查找元素

来自分类Dev

selenium python 通过 href 文本查找链接并单击它

来自分类Dev

通过python正则表达式爬行网页

来自分类Dev

通过python正则表达式爬行网页

来自分类Dev

无法在python中使用Selenium WebDriver单击链接

来自分类Dev

迭代期间无法获取所有必要的链接-Selenium Python

来自分类Dev

Selenium / python-无法找到部分链接文本

来自分类Dev

无法在Python中使用Selenium获得元素链接

来自分类Dev

单击链接时Elif无法运行Python Selenium

来自分类Dev

无法在python中使用Selenium WebDriver单击链接

来自分类Dev

迭代期间无法获取所有必要的链接-Selenium Python

来自分类Dev

Selenium / python-无法找到部分链接文本

来自分类Dev

无法通过硒python单击表中的超链接

来自分类Dev

如何使Python和Selenium通过将链接与用户输入的文本进行匹配来单击angularjs链接?

来自分类常见问题

使用Selenium和Python无法通过xpath定位元素

来自分类Dev

Python + Selenium-无法通过类名找到元素

来自分类Dev

使用Selenium和Python无法通过xpath定位元素

来自分类Dev

TypeError:无法通过Python使用Selenium调用'str'对象

来自分类Dev

python:selenium webscraping 脚本无法通过模块工作

来自分类Dev

使用python爬行页面

来自分类Dev

用Python爬行WoS

来自分类Dev

Python Selenium单击按钮链接

来自分类Dev

python selenium单击链接按钮

来自分类Dev

等待链接加载[Selenium / python]

来自分类Dev

Python Selenium单击按钮链接

来自分类Dev

使用Selenium和Python抓取网站时无法找到分页链接

Related 相关文章

  1. 1

    Python爬行Pastebin(JavaScript呈现的网页)

  2. 2

    无法单击带有Python Selenium的链接

  3. 3

    Selenium:无法通过XPATH Python查找元素

  4. 4

    如何使用Selenium Python通过链接文本查找元素

  5. 5

    selenium python 通过 href 文本查找链接并单击它

  6. 6

    通过python正则表达式爬行网页

  7. 7

    通过python正则表达式爬行网页

  8. 8

    无法在python中使用Selenium WebDriver单击链接

  9. 9

    迭代期间无法获取所有必要的链接-Selenium Python

  10. 10

    Selenium / python-无法找到部分链接文本

  11. 11

    无法在Python中使用Selenium获得元素链接

  12. 12

    单击链接时Elif无法运行Python Selenium

  13. 13

    无法在python中使用Selenium WebDriver单击链接

  14. 14

    迭代期间无法获取所有必要的链接-Selenium Python

  15. 15

    Selenium / python-无法找到部分链接文本

  16. 16

    无法通过硒python单击表中的超链接

  17. 17

    如何使Python和Selenium通过将链接与用户输入的文本进行匹配来单击angularjs链接?

  18. 18

    使用Selenium和Python无法通过xpath定位元素

  19. 19

    Python + Selenium-无法通过类名找到元素

  20. 20

    使用Selenium和Python无法通过xpath定位元素

  21. 21

    TypeError:无法通过Python使用Selenium调用'str'对象

  22. 22

    python:selenium webscraping 脚本无法通过模块工作

  23. 23

    使用python爬行页面

  24. 24

    用Python爬行WoS

  25. 25

    Python Selenium单击按钮链接

  26. 26

    python selenium单击链接按钮

  27. 27

    等待链接加载[Selenium / python]

  28. 28

    Python Selenium单击按钮链接

  29. 29

    使用Selenium和Python抓取网站时无法找到分页链接

热门标签

归档