python爬虫如何运行在web

阅读: 评论:0

python爬虫如何运行在web

python爬虫如何运行在web

概述:

python通过selenium爬取数据是很多突破封锁的有效途径。但在使用selenium中会遇到很多问题,本文就通过一问一答的形式来通熟易懂的普及如何通过selenium执行javascript程序,进而获取动态执行后的网页。如果你喜欢,欢迎转发本文。

Python资源共享群:484031800

python爬虫编程:用selenium执行javascript出错了,该咋改?

问题:

小明开始学习python爬虫编程了,仿佛整个互联网的数据都快被他纳入囊中了。今天,他又试图完成一个高难度动作,他想让selenium中抓取到以下HTML后,并自动执行js脚本,模仿鼠标自动执行一个点击动作。但令他很失望的是,居然,居然,没用!

Nyaralego

,

Sikonge

,

Ab-Titchaz

and

11 others

like this.

这是他执行的代码。

它没用,没有反应。究竟做错了什么?

Python大大的答案:

要点回答:

使用selenium查找元素并将其传递execute_script()给单击:

link = browser.find_element_by_xpath('//div[@class="vbseo_liked"]/a[contains(@onclick, "hers_click(this)")]')

如果要从头解决这问题,那么以下就是需要了解它的一系列事情:如何使用JavaScript模拟点击?

这就是我做的东西。这很简单,但它有效:

function eventFire(el, etype){

if (el.fireEvent) {

el.fireEvent('on' + etype);

} else {

var evObj = ateEvent('Events');

evObj.initEvent(etype, true, false);

el.dispatchEvent(evObj);

}

}

用法:

ElementById('mytest1'), 'click');如何在Python里进行模拟点击呢?首先制定一个自定义的预期条件,等待元素被“执行”:

class wait_for_text_not_to_end_with(object):

def __init__(self, locator, text):

self.locator = locator

< = text

def __call__(self, driver):

try :

element_text = EC._find_element(driver, self.locator).text.strip()

return not )

except StaleElementReferenceException:

return False

定义完毕后,如何在程序里调用这个类呢?看看以下代码:

from selenium import webdriver

ptions import StaleElementReferenceException

from selenium.webdrivermon.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

< = text

def __call__(self, driver):

try :

element_text = EC._find_element(driver, self.locator).text.strip()

return not )

except StaleElementReferenceException:

return False

browser = webdriver.PhantomJS()

browser.maximize_window()

<(".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, "hers_click(this)")]')

link.click()

function eventFire(el, etype){

if (el.fireEvent) {

el.fireEvent('on' + etype);

} else {

var evObj = ateEvent('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来爬取数据就是这么简单。要点掌握好,开始编制自己的爬虫吧。

本文发布于:2024-01-30 15:50:21,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170660102321114.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:爬虫   python   web
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23