python抓取动态网页(Python抓取框架Scrapy快速入门教程》的运行流程及运行)

优采云 发布时间: 2021-09-16 05:18

  python抓取动态网页(Python抓取框架Scrapy快速入门教程》的运行流程及运行)

  Selenium是一套用于测试Web UI的自动化测试框架。它通过调用chrome和Firefox来完成动态页面(包括JavaScript)的加载,因此它还可以用来完成动态网页捕获

  1、下载硒

  wget http://selenium-release.storage.googleapis.com/2.44/selenium-server-standalone-2.44.0.jar

  2、start-selenium-standalone

  java -jar ./selenium-server-standalone-2.44.0.jar -singleWindow

  上面启动的是一个web服务器,它可以接受URL字符串,然后在自己的过程中启动浏览器,解析并与客户端交互,并根据需要将解析结果返回给客户端

  3、安装硒

  sudo apt-get install python-selenium

  这个包是一个用于selenium客户机的python包。当然,您也可以手动安装pypi或pip

  文档可以在这里找到

  4、代码

  from selenium import webdriver

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

# Init selenium replace the ip port with your own selenium-standalone

driver = webdriver.Remote(

command_executor='http://127.0.0.1:4444/wd/hub',

desired_capabilities=DesiredCapabilities.FIREFOX

)

# Remote get dynamic page on selenium-standalone

try:

driver.set_page_load_timeout(5)

driver.get(response.url)

except:

pass

# Parse

for tag in driver.find_elements_by_xpath(xpath):

# ...process

pass

# Cleanup selenium

driver.close()

  5、用于非X11环境

  这可以通过xvfb实现。这是一个模拟xserver的程序。它可以在没有xserver的机器上运行来自xserver服务的程序

  sudo apt-get install xvfb

  跑

  xvfb-run java -jar ./selenium-server-standalone-2.44.0.jar -singleWindow

  6、关于与刮痧结合

  请参考我之前写的文章“Python捕获框架scripy的快速入门教程”。这并不复杂

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线