Python爬虫很强大,在爬虫里如何自动操控浏览器呢?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python爬虫很强大,在爬虫里如何自动操控浏览器呢?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4312字,纯文字阅读大概需要7分钟。
内容图文
![Python爬虫很强大,在爬虫里如何自动操控浏览器呢?](/upload/InfoBanner/zyjiaocheng/741/7d1ca2ef6cb445178bc1a38e1f8b0960.jpg)
概述:
python通过selenium爬取数据是很多突破封锁的有效途径。但在使用selenium中会遇到很多问题,本文就通过一问一答的形式来通熟易懂的普及如何通过selenium执行javascript程序,进而获取动态执行后的网页。如果你喜欢,欢迎转发本文。
Python资源共享群:484031800
python爬虫编程:用selenium执行javascript出错了,该咋改?
问题:
小明开始学习python爬虫编程了,仿佛整个互联网的数据都快被他纳入囊中了。今天,他又试图完成一个高难度动作,他想让selenium中抓取到以下HTML后,并自动执行js脚本,模仿鼠标自动执行一个点击动作。但令他很失望的是,居然,居然,没用!
<div class="vbseo_liked"> <a href="http://www.jamiiforums.com/member.php?u=8355" rel="nofollow">Nyaralego</a> , <a href="http://www.jamiiforums.com/member.php?u=8870" rel="nofollow">Sikonge</a> , <a href="http://www.jamiiforums.com/member.php?u=8979" rel="nofollow">Ab-Titchaz</a> and <a οnclick="return vbseoui.others_click(this)" href="http://www.jamiiforums.com/kenyan-news/225589-kenyan-and-tanzanian-surburbs.html#">11 others</a> like this. </div>
这是他执行的代码。
browser.execute_script("document.getElement(By.xpath(\"//div[@class='vbseo_liked']/a[contains(@onclick, 'return vbseoui.others_click(this)')]\").click()")
它没用,没有反应。究竟做错了什么?
Python大大的答案:
要点回答:
使用selenium查找元素并将其传递execute_script()给单击:
link = browser.find_element_by_xpath('//div[@class="vbseo_liked"]/a[contains(@onclick, "return vbseoui.others_click(this)")]') browser.execute_script('arguments[0].click();', link)
如果要从头解决这问题,那么以下就是需要了解它的一系列事情:
- 如何使用JavaScript模拟点击?
这就是我做的东西。这很简单,但它有效:
function eventFire(el, etype){ if (el.fireEvent) { el.fireEvent('on' + etype); } else { var evObj = document.createEvent('Events'); evObj.initEvent(etype, true, false); el.dispatchEvent(evObj); } }
用法:
eventFire(document.getElementById('mytest1'), 'click');
- 如何在Python里进行模拟点击呢?首先制定一个自定义的预期条件,等待元素被“执行”:
class wait_for_text_not_to_end_with(object): def __init__(self, locator, text): self.locator = locator self.text = text def __call__(self, driver): try : element_text = EC._find_element(driver, self.locator).text.strip() return not element_text.endswith(self.text) except StaleElementReferenceException: return False
定义完毕后,如何在程序里调用这个类呢?看看以下代码:
from selenium import webdriver from selenium.common.exceptions import StaleElementReferenceException from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC class wait_for_text_not_to_end_with(object): def __init__(self, locator, text): self.locator = locator self.text = text def __call__(self, driver): try : element_text = EC._find_element(driver, self.locator).text.strip() return not element_text.endswith(self.text) except StaleElementReferenceException: return False browser = webdriver.PhantomJS() browser.maximize_window() browser.get("http://www.jamiiforums.com/kenyan-news/225589-kenyan-and-tanzanian-surburbs.html") username = browser.find_element_by_id("navbar_username") password = browser.find_element_by_name("vb_login_password_hint") username.send_keys("MarioP") password.send_keys("codeswitching") browser.find_element_by_class_name("loginbutton").click() wait = WebDriverWait(browser, 30) wait.until(EC.visibility_of_element_located((By.XPATH, '//h2[contains(., "Redirecting")]'))) wait.until(EC.title_contains('Kenyan & Tanzanian')) wait.until(EC.visibility_of_element_located((By.ID, 'postlist'))) # click "11 others" link link = browser.find_element_by_xpath('//div[@class="vbseo_liked"]/a[contains(@onclick, "return vbseoui.others_click(this)")]') link.click() browser.execute_script(""" function eventFire(el, etype){ if (el.fireEvent) { el.fireEvent('on' + etype); } else { var evObj = document.createEvent('Events'); evObj.initEvent(etype, true, false); el.dispatchEvent(evObj); } } eventFire(arguments[0], "click"); """, link) # wait for the "div" not to end with "11 others link this." wait.until(wait_for_text_not_to_end_with((By.CLASS_NAME, 'vbseo_liked'), "11 others like this.")) print 'success!!' browser.close()
看,如何在python里通过selenium来爬取数据就是这么简单。要点掌握好,开始编制自己的爬虫吧。
内容总结
以上是互联网集市为您收集整理的Python爬虫很强大,在爬虫里如何自动操控浏览器呢?全部内容,希望文章能够帮你解决Python爬虫很强大,在爬虫里如何自动操控浏览器呢?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。