zoukankan      html  css  js  c++  java
  • python爬虫使用requests请求无法获取网页元素时终极解决方案

        爬取数据时,有时候会出现无法通过正常的requests请求获取网页内容,导致数据无法抓取到,遇到这种情况时,可以换种思路去爬取数据,使用PhantomJS,即爬虫终极解决方案去获取页面元素。

    #!/usr/local/bin/python3.7
    
    from selenium import webdriver
    import time
    
    # phantomJS路径
    path = '/Users/mozili/Documents/PhantomJS/phantomjs-2.1.1-macosx/bin/phantomjs'
    # 创建浏览器对象
    browser = webdriver.PhantomJS(path)
    
    # 打开百度并操作
    url = 'https://www.baidu.com'
    browser.get(url)
    time.sleep(1)
    # 截图
    browser.save_screenshot('Reptile/phantomjs_img/baidu.png')
    # 定位搜索框
    search = browser.find_element_by_id('kw')
    time.sleep(1)
    # 在搜索框输入内容
    search.send_keys('美女')
    time.sleep(1)
    # 截图
    browser.save_screenshot('Reptile/phantomjs_img/meinv.png')
    # 关闭浏览器
    browser.quit()
  • 相关阅读:
    Linux 文件、目录与磁盘格式
    前端初始-HTML
    图片验证码
    note_Set
    MVC登出友情提示
    c#后台调用API
    服务器上调用 批处理、程序集的那些事
    客户懂点代码是最致命的毒药
    Json基础
    多条目日志记录小结
  • 原文地址:https://www.cnblogs.com/lxmtx/p/13126534.html
Copyright © 2011-2022 走看看