码迷,mamicode.com
首页 > 编程语言 > 详细

python爬虫 Selenium库学习

时间:2017-12-03 20:52:48      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:name   两种方法   别人   nal   key   tag   sele   port   mod   

一、自动化测试工具,支持多种浏览器,解决JS渲染问题

二、安装

pip3 install Selenium

三、操作介绍(因为是学习别人的课程为了尊重知识产权,部分代码就不显示了)

1驱动浏览器

browser = webdriver.Chrome()

try:   
 browser.get(‘www.sina.com‘)#上网

2查找元素 

一种方法:

browser.find_element_by_name()
browser.find_element_by_class_name()
browser.find_element_by_id()
browser.find_element_by_xpath()
browser.find_element_by_tag_name()

等等

第二种方法:

browser.find_element(By.NAME,‘search‘)

这里面的find_element相当于BS4里面的find,如果想要用find_all,就要用find_elements

同上有两种方法,我这里就写一种了

browser.find_elements(By.NAME,‘search‘)

3交互操作

input = browser.find_element_by_class_name(‘search-combobox-input‘)
input.send_keys(‘机械键盘‘)
time.sleep(3)
input.clear()
input.send_keys(‘iPad‘)
time.sleep(3)
button = browser.find_element_by_class_name(‘btn-search‘)
button.click()

更多操作详见官方文档:

http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.remote.webelement

执行动作链:

感觉这用的不多,详见官方文档

http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.action_chains

4获取属性 

显示属性

input = browser.find_element_by_class_name(‘search-combobox-input‘)

print(input.get_attribute(‘aria-combobox‘))

这样返回的是aria-combobox 的属性‘list’

显示文本

browser = webdriver.Chrome()
try:
url = ‘https://www.zhihu.com/explore‘
browser.get(url)
answers = browser.find_elements_by_class_name(‘zh-summary‘)
for answer in answers:
print(answer.text)
finally:
browser.close()

这样就显示了知乎发现里面答案的文本

获取ID、位置、标签名、大小

browser = webdriver.Chrome()
try:
url = ‘https://www.zhihu.com/explore‘
browser.get(url)
answers = browser.find_elements_by_class_name(‘zh-summary‘)
for answer in answers:

print(answer.location)
print(answer.tag_name)
print(answer.size)

finally:
browser.close()

Frame

在某些用到框架的网页里面使用:

browser.switch_to.frame(‘name‘)
browser.switch_to.parent_frame(‘name‘)

5等待

有的时候网络需要等待Ajax请求加载,所以需要等待网页全部加载出来

隐式等待

当加载网页没有显示我们需要找的元素的时候,会隐式地等待一段时,如果找到元素就直接返回

browser = webdriver.Chrome()

browser.implicitly_wait(10)

url = ‘https://www.zhihu.com/explore‘
browser.get(url)

显式等待

显式等待是如果特定条件不满足则等待规定的时间

browser = webdriver.Chrome()
url = ‘https://www.zhihu.com/explore‘
browser.get(url)
wait = WebDriverWait(browser,10)
intput = wait.until(EC.presence_of_element_located((By.ID,‘q‘)))
button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,‘***‘)))

显式等待的条件见官方文档:http://selenium-python.readthedocs.io/api.html#selenium.webdriver.support.expected_conditions.element_located_selection_state_to_be

title_is标题是某内容
title_contains标题包含某内容
presence_of_all_elements_located y元素加载出,传入定位元组,如(By.ID,‘p‘)
visibility_of 元素可见,传入定位元组

text_to_be_present_in_element 某个元素文本包含某个文字
text_to_be_present_in_element_value 某个元素值包含某文件
element_to_be_clickable 某元素可点击

等等

6浏览器前进和后退

browser.forward()
browser.back()

7Cookies

可以进行cookies的查看,添加和删除操作

url = ‘https://www.zhihu.com/explore‘
browser.get(url)
print(browser.get_cookies())
browser.add_cookie({‘name‘:‘dai‘,‘value‘:‘123‘})#一定要记得有value否则会报错
print(browser.get_cookies())
browser.delete_all_cookies()
print(browser.get_cookies())

python爬虫 Selenium库学习

标签:name   两种方法   别人   nal   key   tag   sele   port   mod   

原文地址:http://www.cnblogs.com/daibigmonster/p/7965459.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!