-
Notifications
You must be signed in to change notification settings - Fork 0
/
lect04_selenium.py
66 lines (57 loc) · 2.04 KB
/
lect04_selenium.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
作者:倪媛
功能:selenium模拟浏览器抓取
日期:23/10/2018
"""
from selenium import webdriver
import time
def main():
"""
主函数
"""
# # url = 'http://www.santostang.com/2018/07/04/hello-world/'
# # driver = webdriver.Firefox()
# # driver.get(url, timeout=30)
# caps = webdriver.DesiredCapabilities().FIREFOX
# caps['marionette'] = True
#
# binary = FirefoxBinary(r'C:\Program Files\Mozilla Firefox\firefox.exe')
# driver = webdriver.Firefox(firefox_binary=binary, capabilities=caps)
# driver.get('http://www.santostang.com/2018/07/04/hello-world/')
# 控制图片加载
# chrome_options = webdriver.ChromeOptions()
# prefs = {'profile.default_content_setting_values':
# {
# 'images': 2,
# 'javascript': 2
# }
# }
# chrome_options.add_experimental_option('prefs', prefs)
# driver = webdriver.Chrome(chrome_options=chrome_options)
driver = webdriver.Chrome()
# 窗口最大化
driver.maximize_window()
# 设置等待超时时间
driver.implicitly_wait(30)
url = 'http://www.santostang.com/2018/07/04/hello-world/'
driver.get(url)
# print(driver.page_source)
driver.switch_to.frame(driver.find_element_by_css_selector('iframe[title=''livere'']'))
# print(driver.page_source)
# 查询出满足条件的第一个元素
# comment = driver.find_element_by_css_selector('div.reply-content')
# content = comment.find_element_by_tag_name('p')
# print(content.text)
# 循环点击查看更多
for i in range(4):
driver.find_element_by_css_selector('button.more-btn').click()
# 等待3秒
time.sleep(3)
# 查询出满足条件的所有元素,这里的comments是一个list
comments = driver.find_elements_by_class_name('reply-content')
print(type(comments))
for comment in comments:
content = comment.find_element_by_tag_name('p')
print(content.text)
if __name__ == '__main__':
main()