Python爬虫 | Selenium详解
<h2><span style="font-size: 18pt; font-family: "Microsoft YaHei""><strong>一、简介</strong></span></h2><p><span style="font-size: 18px; font-family: "Microsoft YaHei"">网页三元素:</span></p>
<ul>
<li><span style="font-size: 18px; font-family: "Microsoft YaHei"">html负责<strong>内容</strong>;</span></li>
<li><span style="font-size: 18px; font-family: "Microsoft YaHei"">css负责<strong>样式</strong>;</span></li>
<li><span style="font-size: 18px; font-family: "Microsoft YaHei"">JavaScript负责<strong>动作</strong>;</span></li>
</ul>
<p><span style="font-size: 18px; font-family: "Microsoft YaHei""> </span></p>
<p><span style="font-size: 18px; font-family: "Microsoft YaHei"">从数据的角度考虑,网页上呈现出来的数据的来源:</span></p>
<ul>
<li><span style="font-size: 18px; font-family: "Microsoft YaHei"">html文件</span></li>
<li><span style="font-size: 18px; font-family: "Microsoft YaHei"">ajax接口</span></li>
<li><span style="font-size: 18px; font-family: "Microsoft YaHei"">javascript加载</span></li>
</ul>
<p><span style="font-size: 18px; font-family: "Microsoft YaHei""> </span></p>
<blockquote>
<p><span style="font-family: "Microsoft YaHei"; font-size: 16px">如果用requests对一个页面发送请求,只能获得当前加载出来的部分页面,动态加载的数据是获取不到的,比如下拉滚轮得到的数据。selenium最初是一个自动化测试工具, 而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题。selenium本质是通过驱动浏览器,完全模拟浏览器的操作,比如跳转、输入、点击、下拉等,来拿到网页渲染之后的结果,可支持多种浏览器。Selenium是python的一个第三方库,对外提供的接口可以操作浏览器,然后让浏览器完成自动化的操作。</span></p>
</blockquote>
<p><span style="font-size: 18px; font-family: "Microsoft YaHei"">selenium在爬虫中的应用:</span></p>
<ul>
<li><span style="font-size: 18px; font-family: "Microsoft YaHei"">模拟登录</span></li>
<li><span style="font-size: 18px; font-family: "Microsoft YaHei"">便捷的获取动态加载的数据</span></li>
</ul>
<p><span style="font-size: 18px; font-family: "Microsoft YaHei"">缺点:</span></p>
<ul>
<li><span style="font-size: 18px; font-family: "Microsoft YaHei"">爬取数据的效率底</span></li>
<li><span style="font-size: 18px; font-family: "Microsoft YaHei"">环境部署繁琐</span></li>
</ul>
<h2><span style="font-family: "Microsoft YaHei"; font-size: 18pt"><strong>二、</strong><strong>环境安装</strong></span></h2>
<ul>
<li><span style="font-size: 18px; font-family: "Microsoft YaHei"">下载安装selenium:pip install selenium</span></li>
<li><span style="font-size: 18px; font-family: "Microsoft YaHei"">下载浏览器驱动程序:http://chromedriver.storage.googleapis.com/index.html</span></li>
<li><span style="font-size: 18px; font-family: "Microsoft YaHei"">查看驱动和浏览器版本的映射关系: https://www.cnblogs.com/Summer-skr--blog/p/11715259.html</span></li>
</ul>
<p><span style="font-family: "Microsoft YaHei"; font-size: 18px"><span style="font-family: "Microsoft YaHei"; font-size: 18px"><img src="https://img2018.cnblogs.com/blog/1518468/201909/1518468-20190909125119019-648223781.png"></span></span></p>
<p> </p>
<p><span style="font-family: "Microsoft YaHei"; font-size: 18px"><span style="font-family: "Microsoft YaHei"; font-size: 18px">查看谷歌浏览器版本</span></span></p>
<p><span style="font-family: "Microsoft YaHei"; font-size: 18px"><span style="font-family: "Microsoft YaHei"; font-size: 18px"><img src="https://img2018.cnblogs.com/blog/1518468/201909/1518468-20190909125149624-59623321.png"></span></span></p>
<p> </p>
<p> </p>
<p> </p>
<p> <img src="https://img2018.cnblogs.com/blog/1518468/201909/1518468-20190909125207030-1160709165.png"></p>
<p> </p>
<p> <img src="https://img2018.cnblogs.com/blog/1518468/201909/1518468-20190909125228339-271920080.png"></p>
<p><span style="font-family: "Microsoft YaHei"; font-size: 18px">下载好以后,就有驱动程序了。</span></p>
<p><span style="font-size: 18px; font-family: "Microsoft YaHei""> </span></p>
<h2><span style="font-size: 18pt; font-family: "Microsoft YaHei"">三、基本使用</span></h2>
<p><span style="font-size: 18px; font-family: "Microsoft YaHei""><strong>1.</strong><strong>浏览器创建</strong></span></p>
<p class="p"><span style="font-size: 18px; font-family: "Microsoft YaHei"">Selenium支持非常多的浏览器,如Chrome、Firefox、Edge等,还有Android、BlackBerry等手机端的浏览器。另外,也支持无界面浏览器PhantomJS。</span></p>
<div class="cnblogs_code">
<pre><strong><span style="color: rgba(0, 0, 255, 1)">from</span> selenium <span style="color: rgba(0, 0, 255, 1)">import</span></strong><span style="color: rgba(0, 0, 0, 1)"><strong> webdriver</strong>
browser </span>=<span style="color: rgba(0, 0, 0, 1)"><strong> webdriver.Chrome()</strong>
browser </span>=<span style="color: rgba(0, 0, 0, 1)"> webdriver.Firefox()
browser </span>=<span style="color: rgba(0, 0, 0, 1)"> webdriver.Edge()
browser </span>=<span style="color: rgba(0, 0, 0, 1)"> webdriver.PhantomJS()
browser </span>=<span style="color: rgba(0, 0, 0, 1)"> webdriver.Safari()
browser.quit() </span><span style="color: rgba(255, 0, 0, 1)"><strong># 关闭浏览器</strong></span>
browser.close() <span style="color: rgba(255, 0, 0, 1)"><strong># 关闭当前页面</strong> </span></pre>
</div>
<blockquote>
<p><span style="font-size: 18px; font-family: "Microsoft YaHei""><strong><strong> close 只会关闭当前窗口,而 quit 退出驱动并会关闭所有的窗口。</strong></strong></span></p>
</blockquote>
<p><span style="font-size: 18px; font-family: "Microsoft YaHei""><strong>2.打开网页</strong></span></p>
<div class="cnblogs_code">
<pre>browser.get(url) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 打开path路径</span>
page_text = browser.page_source <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 获取当前浏览器页面的源码数据</span></pre>
</div>
<p><span style="font-size: 18px; font-family: "Microsoft YaHei""><strong>3.</strong><strong>元素定位</strong></span></p>
<p class="pre"><span style="font-family: "Microsoft YaHei"; font-size: 18px">查找一个元素(单节点)</span></p>
<div class="cnblogs_code">
<pre>element =<span style="color: rgba(0, 0, 0, 1)"> find_element_by_<span style="color: rgba(255, 0, 0, 1)"><strong>id</strong></span>()
element </span>=<span style="color: rgba(0, 0, 0, 1)"> find_element_by_<span style="color: rgba(255, 0, 0, 1)"><strong>name</strong></span>()
element </span>=<span style="color: rgba(0, 0, 0, 1)"> find_element_by_<strong><span style="color: rgba(255, 0, 0, 1)">class_name</span></strong>()
element </span>=<span style="color: rgba(0, 0, 0, 1)"> find_element_by_tag_name()
element </span>=<span style="color: rgba(0, 0, 0, 1)"> find_element_by_link_text()
element </span>=<span style="color: rgba(0, 0, 0, 1)"> find_element_by_partial_link_text()
element </span>=<span style="color: rgba(0, 0, 0, 1)"> find_element_by_<span style="color: rgba(255, 0, 0, 1)"><strong>xpath</strong></span>()
element </span>= find_element_by_css_selector()</pre>
</div>
<p class="pre"><span style="font-family: "Microsoft YaHei"; font-size: 18px">查找多个元素(多节点)</span></p>
<div class="cnblogs_code">
<pre>element =<span style="color: rgba(0, 0, 0, 1)"> find_elements_by_id()
element </span>=<span style="color: rgba(0, 0, 0, 1)"> find_elements_by_name()
element </span>=<span style="color: rgba(0, 0, 0, 1)"> find_elements_by_class_name()
element </span>=<span style="color: rgba(0, 0, 0, 1)"> find_elements_by_tag_name()
element </span>=<span style="color: rgba(0, 0, 0, 1)"> find_elements_by_link_text()
element </span>=<span style="color: rgba(0, 0, 0, 1)"> find_elements_by_partial_link_text()
element </span>=<span style="color: rgba(0, 0, 0, 1)"> find_elements_by_xpath()
element </span>= find_elements_by_css_selector()</pre>
</div>
<p class="pre"><span style="font-size: 18px; font-family: "Microsoft YaHei"">注意:</span></p>
<p class="p"><span style="font-size: 16px; font-family: "Microsoft YaHei"">(1)find_element_by_xxx第一个符合条件的标签,find_element<span style="background-color: rgba(255, 0, 0, 1)"><strong>s</strong></span>_by_xxx找的是所有符合条件的标签。</span></p>
<p class="p"><span style="font-size: 16px; font-family: "Microsoft YaHei"">(2)根据ID、CSS选择器和XPath获取,它们返回的结果完全一致。</span></p>
<p class="p"><span style="font-size: 16px; font-family: "Microsoft YaHei"">(3)另外,Selenium还提供了通用方法<strong>find_element()</strong>,它需要传入两个参数:查找方式By和值。实际上,它就是find_element_by_id()这种方法的通用函数版本,比如find_element_by_id(id)就等价于find_element(By.ID, id),二者得到的结果完全一致。</span> </p>
<div class="cnblogs_code">
<pre><strong><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 通过id定位</span></strong>
<html>
<body>
<form <strong>id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">loginForm</span><span style="color: rgba(128, 0, 0, 1)">"</span></strong>>
<input name=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">username</span><span style="color: rgba(128, 0, 0, 1)">"</span> type=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">text</span><span style="color: rgba(128, 0, 0, 1)">"</span> />
<input name=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">password</span><span style="color: rgba(128, 0, 0, 1)">"</span> type=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">password</span><span style="color: rgba(128, 0, 0, 1)">"</span> />
<input name=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">continue</span><span style="color: rgba(128, 0, 0, 1)">"</span> type=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">submit</span><span style="color: rgba(128, 0, 0, 1)">"</span> value=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Login</span><span style="color: rgba(128, 0, 0, 1)">"</span> />
</form>
</body>
<html><span style="color: rgba(0, 0, 0, 1)">
login_form </span>= driver.find_element_by_id(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">loginForm</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
<br><br>
</span><strong><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 通过name定位</span></strong>
<html>
<body>
<form id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">loginForm</span><span style="color: rgba(128, 0, 0, 1)">"</span>>
<input <strong>name=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">username</span><span style="color: rgba(128, 0, 0, 1)">"</span></strong> type=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">text</span><span style="color: rgba(128, 0, 0, 1)">"</span> />
<input <strong>name=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">password</span><span style="color: rgba(128, 0, 0, 1)">"</span></strong> type=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">password</span><span style="color: rgba(128, 0, 0, 1)">"</span> />
<input name=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">continue</span><span style="color: rgba(128, 0, 0, 1)">"</span> type=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">submit</span><span style="color: rgba(128, 0, 0, 1)">"</span> value=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Login</span><span style="color: rgba(128, 0, 0, 1)">"</span> />
<input name=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">continue</span><span style="color: rgba(128, 0, 0, 1)">"</span> type=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">button</span><span style="color: rgba(128, 0, 0, 1)">"</span> value=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Clear</span><span style="color: rgba(128, 0, 0, 1)">"</span> />
</form>
</body>
<html><span style="color: rgba(0, 0, 0, 1)">
username </span>= driver.find_element_by_name(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">username</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
password </span>= driver.find_element_by_name(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">password</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
<br><br>
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 通过链接文本定位</span>
<html>
<body>
<p>Are you sure you want to do this?</p>
<a <strong>href=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">continue.html</span><span style="color: rgba(128, 0, 0, 1)">"</span></strong>>Continue</a>
<a href=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">cancel.html</span><span style="color: rgba(128, 0, 0, 1)">"</span>>Cancel</a>
</body>
<html><span style="color: rgba(0, 0, 0, 1)">
continue_link </span>= driver.find_element_by_link_text(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Continue</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
continue_link </span>= driver.find_element_by_partial_link_text(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Conti</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
<br><br>
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 通过标签名定位</span>
<html>
<body>
<span style="color: rgba(255, 0, 0, 1)"> <strong><h1></strong></span>Welcome<span style="color: rgba(255, 0, 0, 1)"><strong></h1></strong></span>
<p>Site content goes here.</p>
</body>
<html><span style="color: rgba(0, 0, 0, 1)">
heading1 </span>= driver.find_element_by_tag_name(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">h1</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
<br><br>
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 通过类名定位</span>
<html>
<body>
<p <strong><span style="color: rgba(0, 0, 255, 1)">class</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">content</span><span style="color: rgba(128, 0, 0, 1)">"</span></strong>>Site content goes here.</p>
</body>
<html><span style="color: rgba(0, 0, 0, 1)">
content </span>= driver.find_element_by_class_name(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">content</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
<br><br>
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 通过CSS选择器定位</span>
<html>
<body>
<p <span style="color: rgba(0, 0, 255, 1)">class</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">content</span><span style="color: rgba(128, 0, 0, 1)">"</span>>Site content goes here.</p>
</body>
<html><span style="color: rgba(0, 0, 0, 1)">
content </span>= driver.find_element_by_css_selector(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">p.content</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 推荐使用xpath定位</span>
username = driver.find_element_by_xpath(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">//form</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
username </span>= driver.find_element_by_xpath(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">//form[@id='loginForm']/input</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
username </span>= driver.find_element_by_xpath(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">//input[@name='username']</span><span style="color: rgba(128, 0, 0, 1)">"</span>)</pre>
</div>
<p class="p"><span style="font-size: 18px; font-family: "Microsoft YaHei""><strong>4.</strong></span><strong><span style="font-family: "Microsoft YaHei"; font-size: 18px">节点操作</span></strong> </p>
<p id="autoid-5-4-0"><span style="font-family: "Microsoft YaHei"; font-size: 18px">ele.text 拿到节点的内容 (包括后代节点的所有内容)</span></p>
<div class="cnblogs_code">
<pre>driver.find_element_by_id(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">gin</span><span style="color: rgba(128, 0, 0, 1)">'</span>)<span style="color: rgba(255, 0, 0, 1)"><strong>.text</strong></span> </pre>
</div>
<p id="autoid-5-4-1"><span style="font-family: "Microsoft YaHei"; font-size: 18px">ele.send_keys("")搜索框输入文字</span></p>
<div class="cnblogs_code">
<pre>driver.find_element_by_id(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">kw</span><span style="color: rgba(128, 0, 0, 1)">'</span>)<span style="color: rgba(255, 0, 0, 1)"><strong>.send_keys("Python")</strong></span> </pre>
</div>
<p id="autoid-5-4-2"><span style="font-size: 18px; font-family: "Microsoft YaHei"">ele.click()标签</span></p>
<div class="cnblogs_code">
<pre>driver.find_element_by_id(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">su</span><span style="color: rgba(128, 0, 0, 1)">'</span>)<span style="color: rgba(255, 0, 0, 1)"><strong>.click()</strong></span> </pre>
</div>
<p id="autoid-5-4-3"><span style="font-size: 18px; font-family: "Microsoft YaHei"">ele.get_attribute("")获取属性值</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">获取元素标签的内容</span>
att01 = a.get_attribute(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">textContent</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> # 获取元素内的全部HTML</span>
att02 = a.get_attribute(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">innerHTML</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> # 获取包含选中元素的HTML</span>
att03 = a.get_attribute(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">outerHTML</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 获取该元素的标签类型</span>
tag01 = a_href.tag_name</pre>
</div>
<p class="p"><span style="font-size: 18px; font-family: "Microsoft YaHei""><strong>5.动作链</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">from</span> selenium.webdriver <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> ActionChains
source </span>= browser.find_element_by_css_selector(<span style="color: rgba(128, 0, 0, 1)">''</span><span style="color: rgba(0, 0, 0, 1)">)
target </span>= browser.find_element_by_css_selector(<span style="color: rgba(128, 0, 0, 1)">''</span><span style="color: rgba(0, 0, 0, 1)">)
actions </span>=<span style="color: rgba(0, 0, 0, 1)"> ActionChains(browser)
actions.drag_and_drop(source, target).perform()
actions.release()</span></pre>
</div>
<p class="p"><span style="font-size: 18px; font-family: "Microsoft YaHei""><strong>6.在页面间切换</strong></span></p>
<p class="p"><span style="font-size: 18px; font-family: "Microsoft YaHei"">适用与页面中点开链接出现新的页面的网站,但是浏览器对象browser还是之前页面的对象</span></p>
<div class="cnblogs_code">
<pre>window_handles =<span style="color: rgba(0, 0, 0, 1)"> driver.window_handles
driver.switch_to.window(window_handles[</span>-1])</pre>
</div>
<p class="p"><span style="font-size: 18px; font-family: "Microsoft YaHei""><strong>7.保存网页截图</strong></span></p>
<div class="cnblogs_code">
<pre>driver.save_screenshot(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">screen.png</span><span style="color: rgba(128, 0, 0, 1)">'</span>)</pre>
</div>
<p class="p"><span style="font-size: 18px; font-family: "Microsoft YaHei""><strong>8.</strong><strong>执行JavaScript</strong></span></p>
<div class="cnblogs_code">
<pre>browser.execute_script(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">window.scrollTo(0, document.body.scrollHeight)</span><span style="color: rgba(128, 0, 0, 1)">'</span>)</pre>
</div>
<p><span style="font-size: 18px; font-family: "Microsoft YaHei""><strong>9.</strong><strong>前进和后退</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">browser.back()
browser.forward()</span></pre>
</div>
<p><span style="font-family: "Microsoft YaHei"; font-size: 18px"><strong>10.等待</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">from</span> selenium.webdriver.support.ui <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> WebDriverWait
</span><span style="color: rgba(0, 0, 255, 1)">from</span> selenium.webdriver.support <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> expected_conditions as EC
driver </span>=<span style="color: rgba(0, 0, 0, 1)"> webdriver.Chrome()
driver.get(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">http://somedomain/</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">:
element </span>= WebDriverWait(driver, 10<span style="color: rgba(0, 0, 0, 1)">).until(
EC.presence_of_element_located((By.ID, </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">myDynamicElement</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">))
)
</span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)">:
driver.quit()</span></pre>
</div>
<p><span style="font-family: "Microsoft YaHei"; font-size: 18px">条件</span></p>
<div class="cnblogs_code"><img src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif"><img id="code_img_opened_b0b903b5-2f74-4db4-8c5e-734d809c80d8" class="code_img_opened lazyload" style="display: none" alt="" data-src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif">
<div id="cnblogs_code_open_b0b903b5-2f74-4db4-8c5e-734d809c80d8" class="cnblogs_code_hide">
<pre><span style="color: rgba(0, 0, 0, 1)">title_is
title_contains
presence_of_element_located
visibility_of_element_located
visibility_of
presence_of_all_elements_located
text_to_be_present_in_element
text_to_be_present_in_element_value
frame_to_be_available_and_switch_to_it
invisibility_of_element_located
element_to_be_clickable
staleness_of
element_to_be_selected
element_located_to_be_selected
element_selection_state_to_be
element_located_selection_state_to_be
alert_is_present</span></pre>
</div>
<span class="cnblogs_code_collapse">View Code</span></div>
<p><span style="font-size: 18px; font-family: "Microsoft YaHei""><strong>11.</strong><strong>Cookie处理</strong></span></p>
<p><span style="font-size: 18px; font-family: "Microsoft YaHei"">获取、添加、删除Cookies</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">browser.get_cookies()
browser.add_cookie({</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">domain</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">www.zhihu.com</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">value</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">germey</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">})
browser.delete_all_cookies()</span></pre>
</div>
<p><span style="font-size: 18px; font-family: "Microsoft YaHei""><strong>12. 搜索属性值</strong></span></p>
<ul>
<li><span style="font-size: 18px; font-family: "Microsoft YaHei""><strong>获得element之后搜索</strong></span> </li>
</ul>
<div class="cnblogs_code">
<pre>url = driver.find_element_by_name(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">t2</span><span style="color: rgba(128, 0, 0, 1)">'</span>).get_attribute(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">href</span><span style="color: rgba(128, 0, 0, 1)">'</span>)</pre>
</div>
<ul>
<li><span style="font-size: 18px; font-family: "Microsoft YaHei""><strong>页面源码中搜索</strong></span></li>
</ul>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">源码中搜索字符串,可以是文本值也可以是属性值
res </span>= driver.page_source.find(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">字符串</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
返回值
</span>-1<span style="color: rgba(0, 0, 0, 1)"> 未找到
其他 找到</span></pre>
</div>
<p><span style="font-size: 18px; font-family: "Microsoft YaHei""><strong>13.</strong><strong>谷歌无头浏览器</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">from</span> selenium.webdriver.chrome.options <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> Options。
chrome_options </span>=<span style="color: rgba(0, 0, 0, 1)"> Options()
chrome_options.add_argument(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">--headless</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
chrome_options.add_argument(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">--disable-gpu</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
browser </span>= webdriver.Chrome(executable_path=path, chrome_options=chrome_options)</pre>
</div>
<p><span style="font-size: 18px; font-family: "Microsoft YaHei""><strong>14.</strong><strong>规避监测</strong></span></p>
<p><span style="font-size: 18px; font-family: "Microsoft YaHei"">相关的网站会对selenium发起的请求进行监测,网站后台可以根据window.navigator.webdriver返回值进行selenium的监测,若返回值为undefinded,则不是selenium进行的请求发送;若为true,则是selenium发起的请求。</span></p>
<p><span style="font-size: 18px; font-family: "Microsoft YaHei"">规避监测的方法:</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">from</span> selenium.webdriver <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> ChromeOptions
option </span>=<span style="color: rgba(0, 0, 0, 1)"> ChromeOptions()
option.add_experimental_option(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">excludeSwitches</span><span style="color: rgba(128, 0, 0, 1)">'</span>, [<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">enable-automation</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">])
bro </span>= webdriver.Chrome(executable_path=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">chromedriver.exe</span><span style="color: rgba(128, 0, 0, 1)">'</span>,options=option)</pre>
</div>
<p id="autoid-5-5-2"><strong><span style="font-family: "Microsoft YaHei"; font-size: 18px">15. 切换子框架</span></strong></p>
<p>此操作主要作用与 ifram子框架 的互相切换使用</p>
<div class="cnblogs_code">
<pre>iframe = driver.find_element_by_xxx(<span style="color: rgba(128, 0, 0, 1)">''</span><span style="color: rgba(0, 0, 0, 1)">)
driver.switch_to_frame(节点对象)</span></pre>
</div>
<p id="autoid-5-5-0"><strong><span style="font-family: "Microsoft YaHei"; font-size: 18px">16. 不请求图片模式</span></strong></p>
<p>只需要如下设置则不会请求图片, 会加快效率</p>
<div class="cnblogs_code">
<pre>chrome_opt =<span style="color: rgba(0, 0, 0, 1)"> webdriver.ChromeOptions()
prefs </span>= {<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">profile.managed_default_content_settings.images</span><span style="color: rgba(128, 0, 0, 1)">"</span>: 2<span style="color: rgba(0, 0, 0, 1)">}
chrome_opt.add_experimental_option(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">prefs</span><span style="color: rgba(128, 0, 0, 1)">"</span>, prefs)</pre>
</div>
<p> </p>
<h2>四、鼠标键盘操作(ActionChains)</h2>
<p><strong><span style="font-family: "Microsoft YaHei"; font-size: 18px">1. ActionChains基本用法</span></strong></p>
<p><span style="font-family: "Microsoft YaHei"; font-size: 18px">ActionChains的执行原理:<strong>当你调用ActionChains的方法时,不会立即执行,而是会将所有的操作按顺序存放在一个队列里</strong>,<strong>当你调用perform()方法时,队列中的时间会依次执行</strong></span></p>
<p> </p>
<p><span style="font-family: "Microsoft YaHei"; font-size: 18px">有两种调用方法:</span></p>
<p><strong><span style="font-family: "Microsoft YaHei"; font-size: 18px">链式写法</span></strong></p>
<div class="cnblogs_code">
<pre>menu = driver.find_element_by_css_selector(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">.nav</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
hidden_submenu </span>= driver.find_element_by_css_selector(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">.nav #submenu1</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
<strong>ActionChains(driver).move_to_element(menu).click(hidden_submenu).perform()</strong></span></pre>
</div>
<p><strong><span style="font-family: "Microsoft YaHei"; font-size: 18px">分步写法</span></strong></p>
<div class="cnblogs_code">
<pre>menu = driver.find_element_by_css_selector(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">.nav</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
hidden_submenu </span>= driver.find_element_by_css_selector(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">.nav #submenu1</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
actions </span>=<span style="color: rgba(0, 0, 0, 1)"> ActionChains(driver)
actions.move_to_element(menu)
actions.click(hidden_submenu)
actions.perform()</span></pre>
</div>
<p><span style="font-family: "Microsoft YaHei"; font-size: 18px">两种写法本质是一样的,ActionChains都会按照顺序执行所有的操作。</span></p>
<p><span style="font-family: "Microsoft YaHei"; font-size: 18px"> </span></p>
<p><strong><span style="font-family: "Microsoft YaHei"; font-size: 18px">2. ActionChains方法列表</span></strong></p>
<div class="cnblogs_code">
<pre>click(on_element=<span style="color: rgba(0, 0, 0, 1)">None) ——单击鼠标左键
click_and_hold(on_element</span>=<span style="color: rgba(0, 0, 0, 1)">None) ——点击鼠标左键,不松开
context_click(on_element</span>=<span style="color: rgba(0, 0, 0, 1)">None) ——点击鼠标右键
double_click(on_element</span>=<span style="color: rgba(0, 0, 0, 1)">None) ——双击鼠标左键
send_keys(</span>*<span style="color: rgba(0, 0, 0, 1)">keys_to_send) ——发送某个键到当前焦点的元素
send_keys_to_element(element, </span>*<span style="color: rgba(0, 0, 0, 1)">keys_to_send) ——发送某个键到指定元素
key_down(value, element</span>=<span style="color: rgba(0, 0, 0, 1)">None) ——按下某个键盘上的键
key_up(value, element</span>=<span style="color: rgba(0, 0, 0, 1)">None) ——松开某个键
<strong>drag_and_drop(source, target) ——拖拽到某个元素然后松开
drag_and_drop_by_offset(source, xoffset, yoffset) ——拖拽到某个坐标然后松开
move_by_offset(xoffset, yoffset) ——鼠标从当前位置移动到某个坐标
move_to_element(to_element) ——鼠标移动到某个元素
move_to_element_<span style="color: rgba(255, 0, 0, 1)">with</span>_offset(to_element, xoffset, yoffset) ——移动到距某个元素(左上角坐标)多少距离的位置</strong>
perform() ——执行链中的所有动作
release(on_element</span>=None) ——在某个元素位置松开鼠标左键<span style="font-family: "Microsoft YaHei"; font-size: 18px"> </span></pre>
</div>
<p><strong><span style="font-family: "Microsoft YaHei"; font-size: 18px">3. 代码示例</span></strong></p>
<p><strong><span style="font-family: "Microsoft YaHei"; font-size: 18px">(1)点击操作</span></strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> -*- coding: utf-8 -*-</span>
<span style="color: rgba(0, 0, 255, 1)">from</span> selenium <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> webdriver
</span><span style="color: rgba(0, 0, 255, 1)">from</span> selenium.webdriver.common.action_chains <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> ActionChains
</span><span style="color: rgba(0, 0, 255, 1)">from</span> time <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> sleep
driver </span>=<span style="color: rgba(0, 0, 0, 1)"> webdriver.Firefox()
driver.implicitly_wait(</span>10<span style="color: rgba(0, 0, 0, 1)">)
driver.<span style="color: rgba(255, 0, 0, 1)"><strong>maximize_window()</strong></span>
driver.get(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">http://sahitest.com/demo/clicks.htm</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
click_btn </span>= driver.find_element_by_xpath(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">//input[@value="click me"]</span><span style="color: rgba(128, 0, 0, 1)">'</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 单击按钮</span>
doubleclick_btn = driver.find_element_by_xpath(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">//input[@value="dbl click me"]</span><span style="color: rgba(128, 0, 0, 1)">'</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 双击按钮</span>
rightclick_btn = driver.find_element_by_xpath(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">//input[@value="right click me"]</span><span style="color: rgba(128, 0, 0, 1)">'</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 右键单击按钮</span>
<span style="color: rgba(0, 0, 0, 1)">
ActionChains(driver).click(click_btn).double_click(doubleclick_btn).context_click(rightclick_btn).perform()</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 链式用法</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> driver.find_element_by_name(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">t2</span><span style="color: rgba(128, 0, 0, 1)">'</span>).<span style="color: rgba(255, 0, 0, 1)"><strong>get_attribute('value'</strong></span><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(255, 0, 0, 1)"><strong>)</strong></span>
sleep(</span>2<span style="color: rgba(0, 0, 0, 1)">)
driver.quit()</span></pre>
</div>
<blockquote>
<p><span style="font-size: 16px; color: rgba(255, 0, 0, 1)"><strong><span style="font-family: "Microsoft YaHei""><strong>element.get_attribute()获取某个元素属性</strong><br></span></strong></span></p>
</blockquote>
<p><strong><span style="font-family: "Microsoft YaHei"; font-size: 18px">(2)鼠标移动</span></strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> -*- coding: utf-8 -*-</span>
<span style="color: rgba(0, 0, 255, 1)">from</span> selenium <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> webdriver
</span><span style="color: rgba(0, 0, 255, 1)">from</span> selenium.webdriver.common.action_chains <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> ActionChains
</span><span style="color: rgba(0, 0, 255, 1)">from</span> time <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> sleep
driver </span>=<span style="color: rgba(0, 0, 0, 1)"> webdriver.Firefox()
driver.implicitly_wait(</span>10<span style="color: rgba(0, 0, 0, 1)">)
driver.maximize_window()
driver.get(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">http://sahitest.com/demo/mouseover.htm</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
write </span>= driver.find_element_by_xpath(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">//input[@value="Write on hover"]</span><span style="color: rgba(128, 0, 0, 1)">'</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 鼠标移动到此元素,在下面的input框中会显示“Mouse moved”</span>
blank = driver.find_element_by_xpath(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">//input[@value="Blank on hover"]</span><span style="color: rgba(128, 0, 0, 1)">'</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 鼠标移动到此元素,会清空下面input框中的内容</span>
<span style="color: rgba(0, 0, 0, 1)">
result </span>= driver.find_element_by_name(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">t1</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
action </span>=<span style="color: rgba(0, 0, 0, 1)"> ActionChains(driver)
action.<strong><span style="color: rgba(255, 0, 0, 1)">move_to_element</span></strong>(write).perform() </span><span style="color: rgba(0, 128, 0, 1)">#</span><strong><span style="color: rgba(0, 128, 0, 1)"><span style="color: rgba(255, 0, 0, 1)"> 移动到write</span>,显示“Mouse moved”</span></strong>
<span style="color: rgba(0, 0, 255, 1)">print</span> result.get_attribute(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">value</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> action.move_to_element(blank).perform()</span>
action.<strong><span style="color: rgba(255, 0, 0, 1)">move_by_offset(10, 50)</span></strong>.perform() <span style="color: rgba(255, 0, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"><span style="color: rgba(255, 0, 0, 1)"><strong> 移动到距离当前位置(10,50)的点</strong></span>,与上句效果相同,移动到blank上,清空</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> result.get_attribute(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">value</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
action.<strong><span style="color: rgba(255, 0, 0, 1)">move_to_element_with_offset(blank, </span></strong></span><strong><span style="color: rgba(255, 0, 0, 1)">10, -40)</span></strong>.perform() <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"><span style="color: rgba(255, 0, 0, 1)"><strong> 移动到距离blank元素(10,-40)的点</strong></span>,可移动到write上</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> result.get_attribute(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">value</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
sleep(</span>2<span style="color: rgba(0, 0, 0, 1)">)</span></pre>
</div>
<p><strong><span style="font-family: "Microsoft YaHei"; font-size: 18px">(3)拖拽</span></strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> -*- coding: utf-8 -*-</span>
<span style="color: rgba(0, 0, 255, 1)">from</span> selenium <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> webdriver
</span><span style="color: rgba(0, 0, 255, 1)">from</span> selenium.webdriver.common.action_chains <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> ActionChains
</span><span style="color: rgba(0, 0, 255, 1)">from</span> time <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> sleep
driver </span>=<span style="color: rgba(0, 0, 0, 1)"> webdriver.Firefox()
driver.implicitly_wait(</span>10<span style="color: rgba(0, 0, 0, 1)">)
driver.maximize_window()
driver.get(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">http://sahitest.com/demo/dragDropMooTools.htm</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
dragger </span>= driver.find_element_by_id(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">dragger</span><span style="color: rgba(128, 0, 0, 1)">'</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 被拖拽元素</span>
item1 = driver.find_element_by_xpath(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">//div</span><span style="color: rgba(128, 0, 0, 1)">'</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 目标元素1</span>
item2 = driver.find_element_by_xpath(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">//div</span><span style="color: rgba(128, 0, 0, 1)">'</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 目标2</span>
item3 = driver.find_element_by_xpath(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">//div</span><span style="color: rgba(128, 0, 0, 1)">'</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 目标3</span>
item4 = driver.find_element_by_xpath(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">//div</span><span style="color: rgba(128, 0, 0, 1)">'</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 目标4</span>
<span style="color: rgba(0, 0, 0, 1)">
action </span>=<span style="color: rgba(0, 0, 0, 1)"> ActionChains(driver)
action.<strong><span style="color: rgba(255, 0, 0, 1)">drag_and_drop(dragger, item1)</span></strong>.perform() </span><strong><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 1.移动dragger到item1</span></strong>
sleep(2<span style="color: rgba(0, 0, 0, 1)">)
action.click_and_hold(dragger).release(item2).perform() </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 2.效果与上句相同,也能起到移动效果</span>
sleep(2<span style="color: rgba(0, 0, 0, 1)">)
action.click_and_hold(dragger).move_to_element(item3).release().perform() </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 3.效果与上两句相同,也能起到移动的效果</span>
sleep(2<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> action.<strong><span style="color: rgba(255, 0, 0, 1)">drag_and_drop_by_offset(dragger, 400, 150)</span></strong>.perform() # <strong>4.移动到指定坐标</strong></span>
action.click_and_hold(dragger).move_by_offset(400, 150).release().perform() <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 5.与上一句相同,移动到指定坐标</span>
sleep(2<span style="color: rgba(0, 0, 0, 1)">)
driver.quit()</span></pre>
</div>
<p><span style="font-family: "Microsoft YaHei"; font-size: 18px">一般用坐标定位很少,用上例中的方法1足够了,如果看源码,会发现方法2其实就是方法1中的drag_and_drop()的实现。注意:拖拽使用时注意加等待时间,有时会因为速度太快而失败。</span></p>
<p><strong><span style="font-family: "Microsoft YaHei"; font-size: 18px">(4)按键</span></strong></p>
<p><span style="font-family: "Microsoft YaHei"; font-size: 18px"> 模拟按键有多种方法,能用win32api来实现,能用SendKeys来实现,也可以用selenium的WebElement对象的send_keys()方法来实现,这里ActionChains类也提供了几个模拟按键的方法。</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> -*- coding: utf-8 -*-</span>
<span style="color: rgba(0, 0, 255, 1)">from</span> selenium <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> webdriver
</span><span style="color: rgba(0, 0, 255, 1)">from</span> selenium.webdriver.common.action_chains <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> ActionChains
</span><span style="color: rgba(0, 0, 255, 1)">from</span> time <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> sleep
driver </span>=<span style="color: rgba(0, 0, 0, 1)"> webdriver.Firefox()
driver.implicitly_wait(</span>10<span style="color: rgba(0, 0, 0, 1)">)
driver.maximize_window()
driver.get(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">http://sahitest.com/demo/keypress.htm</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
key_up_radio </span>= driver.find_element_by_id(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">r1</span><span style="color: rgba(128, 0, 0, 1)">'</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 监测按键升起</span>
key_down_radio = driver.find_element_by_id(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">r2</span><span style="color: rgba(128, 0, 0, 1)">'</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 监测按键按下</span>
key_press_radio = driver.find_element_by_id(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">r3</span><span style="color: rgba(128, 0, 0, 1)">'</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 监测按键按下升起</span>
<span style="color: rgba(0, 0, 0, 1)">
enter </span>= driver.find_elements_by_xpath(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">//form[@name="f1"]/input</span><span style="color: rgba(128, 0, 0, 1)">'</span>) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 输入框</span>
result = driver.find_elements_by_xpath(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">//form[@name="f1"]/input</span><span style="color: rgba(128, 0, 0, 1)">'</span>)<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 监测结果</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 监测key_down</span>
<span style="color: rgba(0, 0, 0, 1)">key_down_radio.click()
ActionChains(driver).key_down(Keys.CONTROL, enter).key_up(Keys.CONTROL).perform()
</span><span style="color: rgba(0, 0, 255, 1)">print</span> result.get_attribute(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">value</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 监测key_up</span>
<span style="color: rgba(0, 0, 0, 1)">key_up_radio.click()
enter.click()
ActionChains(driver).key_down(Keys.SHIFT).key_up(Keys.SHIFT).perform()
</span><span style="color: rgba(0, 0, 255, 1)">print</span> result.get_attribute(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">value</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 监测key_press</span>
<span style="color: rgba(0, 0, 0, 1)">key_press_radio.click()
enter.click()
ActionChains(driver).send_keys(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">a</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">).perform()
</span><span style="color: rgba(0, 0, 255, 1)">print</span> result.get_attribute(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">value</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
driver.quit()</span> </pre>
</div>
<p><span style="font-family: "Microsoft YaHei"; font-size: 18px">示例2:</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> -*- coding: utf-8 -*-</span>
<span style="color: rgba(0, 0, 255, 1)">from</span> selenium <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> webdriver
</span><span style="color: rgba(0, 0, 255, 1)">from</span> selenium.webdriver.common.action_chains <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> ActionChains
</span><span style="color: rgba(0, 0, 255, 1)">from</span> selenium.webdriver.common.keys <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> Keys
</span><span style="color: rgba(0, 0, 255, 1)">from</span> time <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> sleep
driver </span>=<span style="color: rgba(0, 0, 0, 1)"> webdriver.Firefox()
driver.implicitly_wait(</span>10<span style="color: rgba(0, 0, 0, 1)">)
driver.maximize_window()
driver.get(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">http://sahitest.com/demo/label.htm</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
input1 </span>= driver.find_elements_by_tag_name(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">input</span><span style="color: rgba(128, 0, 0, 1)">'</span>)
input2 </span>= driver.find_elements_by_tag_name(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">input</span><span style="color: rgba(128, 0, 0, 1)">'</span>)
action </span>=<span style="color: rgba(0, 0, 0, 1)"> ActionChains(driver)
input1.click()
action.send_keys(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Test Keys</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">).perform()
action.key_down(Keys.CONTROL).send_keys(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">a</span><span style="color: rgba(128, 0, 0, 1)">'</span>).key_up(Keys.CONTROL).perform() <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> ctrl+a</span>
action.key_down(Keys.CONTROL).send_keys(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">c</span><span style="color: rgba(128, 0, 0, 1)">'</span>).key_up(Keys.CONTROL).perform() <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> ctrl+c</span>
<span style="color: rgba(0, 0, 0, 1)">
action.key_down(Keys.CONTROL, input2).send_keys(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">v</span><span style="color: rgba(128, 0, 0, 1)">'</span>).key_up(Keys.CONTROL).perform() <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> ctrl+v</span>
<span style="color: rgba(0, 0, 255, 1)">print</span> input1.get_attribute(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">value</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">print</span> input2.get_attribute(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">value</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
driver.quit()</span></pre>
</div>
<p> </p>
<h2>五、<strong>使用示例</strong></h2>
<p><span style="font-size: 18px"><span style="font-family: 微软雅黑">示例</span>1:打开百度,搜索爬虫</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">from</span> selenium <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> webdriver
</span><span style="color: rgba(0, 0, 255, 1)">from</span> time <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> sleep
bro </span>= webdriver.Chrome(<span style="color: rgba(0, 0, 0, 1)">)
bro.get(url</span>=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">https://www.baidu.com/</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
sleep(</span>2<span style="color: rgba(0, 0, 0, 1)">)
text_input </span>= bro.find_element_by_id(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">kw</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
text_input.send_keys(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">爬虫</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
sleep(</span>2<span style="color: rgba(0, 0, 0, 1)">)
bro.find_element_by_id(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">su</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">).click()
sleep(</span>3<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(bro.page_source)
bro.quit()</span></pre>
</div>
<p><span style="font-size: 18px"><span style="font-family: 微软雅黑">示例</span>2:获取豆瓣电影中更多电影详情数据(谷歌无头浏览器)</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">from</span> selenium <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> webdriver
</span><span style="color: rgba(0, 0, 255, 1)">from</span> time <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> sleep
</span><span style="color: rgba(0, 0, 255, 1)">from</span> selenium.webdriver.chrome.options <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> Options
<strong>第1步:下面三行固定</strong>
<strong>chrome_options </strong></span><strong>=<span style="color: rgba(0, 0, 0, 1)"> Options()
chrome_options.add_argument(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">--headless</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
chrome_options.add_argument(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">--disable-gpu</span><span style="color: rgba(128, 0, 0, 1)">'</span></strong><span style="color: rgba(0, 0, 0, 1)"><strong>)</strong>
url </span>= <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">https://movie.douban.com/typerank?type_name=%E6%83%8A%E6%82%9A&type=19&interval_id=100:90&action=</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)"><strong>
第2步:把chrome_options对象作为参数</strong>
bro </span>= webdriver.Chrome(<span style="color: rgba(255, 0, 0, 1)"><strong>chrome_options=</strong></span><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(255, 0, 0, 1)"><strong>chrome_options</strong></span>)
bro.get(url)
sleep(</span>3<span style="color: rgba(0, 0, 0, 1)">)
bro.execute_script(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">window.scrollTo(0,document.body.scrollHeight)</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
sleep(</span>3<span style="color: rgba(0, 0, 0, 1)">)
bro.execute_script(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">window.scrollTo(0,document.body.scrollHeight)</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
sleep(</span>3<span style="color: rgba(0, 0, 0, 1)">)
bro.execute_script(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">window.scrollTo(0,document.body.scrollHeight)</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
sleep(</span>2<span style="color: rgba(0, 0, 0, 1)">)
page_text </span>=<span style="color: rgba(0, 0, 0, 1)"> bro.page_source
with open(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">./douban.html</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">w</span><span style="color: rgba(128, 0, 0, 1)">'</span>,encoding=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">utf-8</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">) as fp:
fp.write(page_text)
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(page_text)
sleep(</span>1<span style="color: rgba(0, 0, 0, 1)">)
bro.quit()</span></pre>
</div>
<p><span style="font-size: 18px"><span style="font-family: 微软雅黑">示例</span>3:<span style="font-family: 微软雅黑">登录</span>qq空间</span></p>
<p><span style="font-size: 18px"><span style="font-family: 微软雅黑"> 在</span>web 中,经常会遇到frame 嵌套页面的应用,使用WebDriver 每次只能在一个页面上识别元素,对于frame 嵌套内的页面上的元素,直接定位是定位是定位不到的。这个时候就需要通过switch_to_frame()方法将当前定位的主体切换了frame 里。<span style="font-family: 微软雅黑">先定位到</span>iframe,再在iframe中进行标签定位。否则,定位不到我们想要的标签。</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> requests
</span><span style="color: rgba(0, 0, 255, 1)">from</span> selenium <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> webdriver
</span><span style="color: rgba(0, 0, 255, 1)">from</span> lxml <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> etree
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> time
driver </span>= webdriver.Chrome(executable_path=r<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">C:\Users\Administrator\chromedriver.exe</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
driver.get(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">https://qzone.qq.com/</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><strong><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">switch_to操作切换frame,此时才能进行登陆页面的操作。</span></strong>
driver.<strong><span style="color: rgba(255, 0, 0, 1)">switch_to.frame('login_frame')</span> </strong> <br> <strong><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)">点击使用账号密码登陆,需要绑定click事件</span></strong>
driver.find_element_by_id(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">switcher_plogin</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">).click()
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">driver.find_element_by_id('u').clear()</span>
driver.find_element_by_id(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">u</span><span style="color: rgba(128, 0, 0, 1)">'</span>).send_keys(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">QQ</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">driver.find_element_by_id('p').clear()</span>
driver.find_element_by_id(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">p</span><span style="color: rgba(128, 0, 0, 1)">'</span>).send_keys(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">密码</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><strong><span style="color: rgba(0, 128, 0, 1)">#</span></strong><span style="color: rgba(0, 128, 0, 1)"><strong>点击登陆,绑定click事件</strong></span>
driver.find_element_by_id(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">login_button</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">).click()
time.sleep(</span>2<span style="color: rgba(0, 0, 0, 1)">)
driver.execute_script(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">window.scrollTo(0,document.body.scrollHeight)</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
time.sleep(</span>2<span style="color: rgba(0, 0, 0, 1)">)
driver.execute_script(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">window.scrollTo(0,document.body.scrollHeight)</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
time.sleep(</span>2<span style="color: rgba(0, 0, 0, 1)">)
driver.execute_script(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">window.scrollTo(0,document.body.scrollHeight)</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
time.sleep(</span>2<span style="color: rgba(0, 0, 0, 1)">)
page_text </span>= driver.page_source <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">获取页面源码数据,注意page_source无括号。</span>
tree = etree.HTML(page_text) <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">执行解析操作</span>
li_list = tree.xpath(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">//ul[@id="feed_friend_list"]/li</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">for</span> li <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> li_list:
text_list </span>= li.xpath(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">.//div[@class="f-info"]//text()|.//div[@class="f-info qz_info_cut"]//text()</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
text </span>= <span style="color: rgba(128, 0, 0, 1)">''</span><span style="color: rgba(0, 0, 0, 1)">.join(text_list)
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(text+<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">\n\n\n</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
driver.quit()</span></pre>
</div>
<p>发现小框是嵌套在大框里面的,在当前的html<span style="font-family: 楷体">源码中,又嵌套了一个</span><span style="font-family: "Times New Roman"">html</span><span style="font-family: 楷体">子页面,这个子页面是包含在</span>iframe<span style="font-family: 楷体">标签中的。所以,</span><strong><span style="font-family: 楷体">如果定位的标签是存在于</span>iframe<span style="font-family: 楷体">中的,那么一定需要使用</span><span style="font-family: "Times New Roman"">switch to</span><span style="font-family: 楷体">函数,将当前浏览器页面的参照物切换到</span><span style="font-family: "Times New Roman"">iframe</span><span style="font-family: 楷体">中,</span><span style="font-family: "Times New Roman"">iframe</span><span style="font-family: 楷体">中有一个</span><span style="font-family: "Times New Roman"">id</span><span style="font-family: 楷体">为</span><span style="font-family: "Times New Roman"">login_frame</span><span style="font-family: 楷体">的属性值,可以根据它来定位。</span></strong></p>
<p><strong><span style="font-family: 楷体"><img alt="" data-src="https://img2018.cnblogs.com/blog/1518468/201909/1518468-20190909161948757-556127463.png"></span></strong></p>
<p><span style="font-size: 18px"><span style="font-family: 微软雅黑">示例</span>4:利用搜狗搜索接口抓取微信公众号(无头、规避检测、等待、切换页面)</span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 添加启动参数 (add_argument)</span><span style="color: rgba(0, 128, 0, 1)">
#</span><span style="color: rgba(0, 128, 0, 1)"> 添加实验性质的设置参数 (add_experimental_option)</span>
<span style="color: rgba(0, 0, 255, 1)">from</span> selenium <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> webdriver
</span><span style="color: rgba(0, 0, 255, 1)">from</span> selenium.webdriver.support.wait <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> WebDriverWait
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> time
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> requests
</span><span style="color: rgba(0, 0, 255, 1)">from</span> lxml <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> etree
option </span>=<span style="color: rgba(0, 0, 0, 1)"> webdriver.ChromeOptions()
option.add_argument(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">headless</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)">设置chromedriver启动参数,规避对selenium的检测机制</span>
<span style="color: rgba(255, 0, 0, 1)"><strong>option.add_experimental_option('excludeSwitches', ['enable-automation'</strong></span><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(255, 0, 0, 1)"><strong>])</strong></span>
driver </span>= webdriver.Chrome(chrome_options=<span style="color: rgba(0, 0, 0, 1)">option)
url </span>= <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">http://weixin.sogou.com/weixin?type=1&s_from=input&query=python_shequ</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
driver.get(url)
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(driver.title)
timeout </span>= 5<span style="color: rgba(0, 0, 0, 1)">
link </span>=<span style="color: rgba(0, 0, 0, 1)"> WebDriverWait(driver, timeout).until(
</span><span style="color: rgba(0, 0, 255, 1)">lambda</span> d: d.find_element_by_link_text(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Python爱好者社区</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">))
link.click()
time.sleep(</span>1<span style="color: rgba(0, 0, 0, 1)">)
</span><strong><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 切换页面</span></strong>
window_handles =<span style="color: rgba(0, 0, 0, 1)"> driver.window_handles
<strong><span style="color: rgba(255, 0, 0, 1)">driver.switch_to.window(window_handles[</span></strong></span><strong><span style="color: rgba(255, 0, 0, 1)">-1])
</span></strong><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(driver.title)
article_links </span>=<span style="color: rgba(0, 0, 0, 1)"> WebDriverWait(driver, timeout).until(
</span><span style="color: rgba(0, 0, 0, 1)"> <strong># EC.presence_of_element_located((By.XPATH, '//h4[@class="weui_media_title"]'))
lambda d: d.find_elements_by_xpath('//h4[@class="weui_media_title"]'</strong><strong>))</strong></span><span style="color: rgba(0, 0, 0, 1)">
article_link_list </span>=<span style="color: rgba(0, 0, 0, 1)"> []
</span><span style="color: rgba(0, 0, 255, 1)">for</span> item <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> article_links:
<strong> article_link </strong></span><strong>= <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">https://mp.weixin.qq.com</span><span style="color: rgba(128, 0, 0, 1)">'</span> + item.get_attribute(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">hrefs</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span></strong><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> print(article_link)</span>
<span style="color: rgba(0, 0, 0, 1)"> article_link_list.append(article_link)
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(article_link_list)
first_article_link </span>=<span style="color: rgba(0, 0, 0, 1)"> article_link_list
header </span>=<span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">User-Agent</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 OPR/26.0.1656.60</span>
<span style="color: rgba(0, 0, 0, 1)"> }
response </span>=<span style="color: rgba(0, 0, 0, 1)"> requests.get(first_article_link,
headers</span>=<span style="color: rgba(0, 0, 0, 1)">header,
timeout</span>=5<span style="color: rgba(0, 0, 0, 1)">
)
tree </span>=<span style="color: rgba(0, 0, 0, 1)"> etree.HTML(response.text)
title </span>= tree.xpath(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">//h2[@id="activity-name"]/text()</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">).strip()
content </span>= tree.xpath(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">//div[@id="js_content"]//text()</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
content </span>= <span style="color: rgba(128, 0, 0, 1)">''</span><span style="color: rgba(0, 0, 0, 1)">.join(content).strip()
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(title)
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(content)</pre>
</div>
<p><span style="font-size: 18px"><span style="font-family: 微软雅黑">示例</span>5:用selenium实现一个头条号的模拟发文接口</span></p>
<div class="cnblogs_code"><img id="code_img_closed_0d313918-d547-446c-af00-9decd2324fc1" class="code_img_closed lazyload" alt="" data-src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif"><img id="code_img_opened_0d313918-d547-446c-af00-9decd2324fc1" class="code_img_opened lazyload" style="display: none" alt="" data-src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif">
<div id="cnblogs_code_open_0d313918-d547-446c-af00-9decd2324fc1" class="cnblogs_code_hide">
<pre><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> time
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> redis
</span><span style="color: rgba(0, 0, 255, 1)">from</span> selenium <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> webdriver
</span><span style="color: rgba(0, 0, 255, 1)">from</span> selenium.webdriver.common.keys <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> Keys
</span><span style="color: rgba(0, 0, 255, 1)">from</span> selenium.webdriver.support.wait <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> WebDriverWait
r </span>= redis.Redis(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">127.0.0.1</span><span style="color: rgba(128, 0, 0, 1)">'</span>, 6379<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> toutiao_save_and_preview(title, content, expand_link):
option </span>=<span style="color: rgba(0, 0, 0, 1)"> webdriver.ChromeOptions()
option.add_argument(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">headless</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
driver </span>= webdriver.Chrome(chrome_options=<span style="color: rgba(0, 0, 0, 1)">option)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 获取渲染的正文</span>
driver.get(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">file:///Users/Documents/toutiao.html</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
driver.execute_script(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">contentIn('</span><span style="color: rgba(128, 0, 0, 1)">"</span>+ content +<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">');</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
timeout </span>= 5<span style="color: rgba(0, 0, 0, 1)">
content_copy </span>= WebDriverWait(driver, timeout).until(<span style="color: rgba(0, 0, 255, 1)">lambda</span> d: d.find_element_by_xpath(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">//button[@class="btn"]</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">))
content_copy.click()
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 模拟登录发文页面</span>
cookie_toutiao = [{<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">ccid</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">value</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">db43e70fd9404338c49209ba04f7a11f</span><span style="color: rgba(128, 0, 0, 1)">'</span>}, {<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">tt_webid</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">value</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">6612748996061414925</span><span style="color: rgba(128, 0, 0, 1)">'</span>}, {<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">UM_distinctid</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">value</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">1667a53d28d449-0e229246a33996-4a506a-1fa400-1667a53d28e361</span><span style="color: rgba(128, 0, 0, 1)">'</span>}, {<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">sso_uid_tt</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">value</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">4c8179804d74252717c675607c721602</span><span style="color: rgba(128, 0, 0, 1)">'</span>}, {<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">toutiao_sso_user</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">value</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">8acc9b248cd201034637248021183d5a</span><span style="color: rgba(128, 0, 0, 1)">'</span>}, {<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">sso_login_status</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">value</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">1</span><span style="color: rgba(128, 0, 0, 1)">'</span>}, {<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">sessionid</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">value</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">8441fa3fc5ae5bc08c3becc780b5b2df</span><span style="color: rgba(128, 0, 0, 1)">'</span>}, {<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">_mp_test_key_1</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">value</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">6aba81df9e257bea2a99713977f1e33b</span><span style="color: rgba(128, 0, 0, 1)">'</span>}, {<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">uid_tt</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">value</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">75b5b52039d4c9dd41315d061c833f0b</span><span style="color: rgba(128, 0, 0, 1)">'</span>}, {<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">ccid</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">value</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">4231c5cd5a98033f2e78336b1809a18a</span><span style="color: rgba(128, 0, 0, 1)">'</span>}, {<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">tt_webid</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">value</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">6631884089946523149</span><span style="color: rgba(128, 0, 0, 1)">'</span>}, {<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">UM_distinctid</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">value</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">16783e1566479-0ae7bcdcaeb592-113b6653-13c680-16783e156656d4</span><span style="color: rgba(128, 0, 0, 1)">'</span>}, {<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">passport_auth_status</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">value</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">99f731f2c6dc150e6dfea46799f20e90</span><span style="color: rgba(128, 0, 0, 1)">'</span>}, {<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">sso_uid_tt</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">value</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">f4bcd2cf972384b428449b0479475ce6</span><span style="color: rgba(128, 0, 0, 1)">'</span>}, {<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">toutiao_sso_user</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">value</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">60df7bb620b4b6d1d17a1de83daec9c1</span><span style="color: rgba(128, 0, 0, 1)">'</span>}, {<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">sso_login_status</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">value</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">1</span><span style="color: rgba(128, 0, 0, 1)">'</span>}, {<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">sessionid</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">value</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">786fe64e9186d51b8427290a557b3c7b</span><span style="color: rgba(128, 0, 0, 1)">'</span>}, {<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">uid_tt</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">value</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">91a7a72a85861ae686fb66177bc16bca</span><span style="color: rgba(128, 0, 0, 1)">'</span>}, {<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__tea_sdk__ssid</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">value</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">60b289e6-e2a4-4494-a3e8-7936f9731426</span><span style="color: rgba(128, 0, 0, 1)">'</span>}, {<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">uuid</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">value</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">w:3ec91cefd76b438583154fea77baa54b</span><span style="color: rgba(128, 0, 0, 1)">'</span>}, {<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">tt_im_token</span><span style="color: rgba(128, 0, 0, 1)">'</span>, <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">value</span><span style="color: rgba(128, 0, 0, 1)">'</span>: <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">1544105894108419437114683515671344747598423336731147829901779697</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">}]
driver.get(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">https://mp.toutiao.com/profile_v3/index</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">for</span> cookie <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> cookie_toutiao:
driver.add_cookie(cookie)
driver.get(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">https://mp.toutiao.com/profile_v3/graphic/publish</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(driver.title)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> driver.maximize_window()</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 写标题</span>
<span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">写标题</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
write_title </span>= WebDriverWait(driver, timeout).until(<span style="color: rgba(0, 0, 255, 1)">lambda</span> d: d.find_element_by_xpath(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">//*[@id="title"]</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">))
write_title.click()
write_title.send_keys(title)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 粘贴正文</span>
<span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">写正文</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
write_content </span>= WebDriverWait(driver, timeout).until(<span style="color: rgba(0, 0, 255, 1)">lambda</span><span style="color: rgba(0, 0, 0, 1)"> d: d.find_element_by_xpath(
</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">//*[@id="graphic"]/div/div/div/div/div/div/div | //div</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">))
write_content.click()
write_content.clear()
write_content.send_keys(Keys.SHIFT </span>+<span style="color: rgba(0, 0, 0, 1)"> Keys.INSERT)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> time.sleep(1)</span>
<span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 检测图片上传是否完成</span>
<span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)">:
</span><span style="color: rgba(0, 0, 255, 1)">if</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">img</span><span style="color: rgba(128, 0, 0, 1)">'</span> <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> content:
WebDriverWait(driver, timeout).until(
</span><span style="color: rgba(0, 0, 255, 1)">lambda</span> d: d.find_element_by_xpath(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">//div[@class="pgc-img-wrapper"]</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">))
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">images uploaded success</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">:
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">no image included</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">except</span><span style="color: rgba(0, 0, 0, 1)">:
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">images uploaded fail</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 页面向下滚动</span>
driver.execute_script(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">window.scrollTo(0, document.body.scrollHeight)</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
time.sleep(</span>1<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 添加扩展链接</span>
expand_check = WebDriverWait(driver, timeout).until(<span style="color: rgba(0, 0, 255, 1)">lambda</span><span style="color: rgba(0, 0, 0, 1)"> d: d.find_element_by_xpath(
</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">//div[@class="pgc-external-link"]//input[@type="checkbox"]</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
))
expand_check.click()
expand_link_box </span>= WebDriverWait(driver, timeout).until(<span style="color: rgba(0, 0, 255, 1)">lambda</span><span style="color: rgba(0, 0, 0, 1)"> d: d.find_element_by_xpath(
</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">//div[@class="link-input"]//input[@type="text"]</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
))
expand_link_box.send_keys(expand_link)
time.sleep(</span>1<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 自动封面</span>
front_img = WebDriverWait(driver, timeout).until(<span style="color: rgba(0, 0, 255, 1)">lambda</span><span style="color: rgba(0, 0, 0, 1)"> d: d.find_element_by_xpath(
</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">//div[@class="article-cover"]/div/div[@class="tui2-radio-group"]/label/div/input</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">,
))
front_img.click()
time.sleep(</span>1<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 保存草稿</span>
save_draft = WebDriverWait(driver, timeout).until(<span style="color: rgba(0, 0, 255, 1)">lambda</span><span style="color: rgba(0, 0, 0, 1)"> d: d.find_element_by_xpath(
</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">//div[@class="publish-footer"]/button/span</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">))
save_draft.click()
time.sleep(</span>1<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 从内容管理页,获取预览链接和文章ID</span>
<span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">get preview_link and article_id</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> driver.refresh()</span>
preview_link = WebDriverWait(driver, timeout).until(<span style="color: rgba(0, 0, 255, 1)">lambda</span><span style="color: rgba(0, 0, 0, 1)"> d: d.find_element_by_xpath(
</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">//div[@id="article-list"]//div[@class="master-title"]/a</span><span style="color: rgba(128, 0, 0, 1)">'</span>)).get_attribute(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">href</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
article_id </span>= preview_link.split(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">=</span><span style="color: rgba(128, 0, 0, 1)">'</span>)[-1<span style="color: rgba(0, 0, 0, 1)">]
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(preview_link, article_id)
time.sleep(</span>1<span style="color: rgba(0, 0, 0, 1)">)
content_management </span>= WebDriverWait(driver, timeout).until(<span style="color: rgba(0, 0, 255, 1)">lambda</span> d: d.find_element_by_link_text(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">内容管理</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">))
content_management.click()
time.sleep(</span>1<span style="color: rgba(0, 0, 0, 1)">)
driver.refresh()
preview_link </span>= WebDriverWait(driver, timeout).until(<span style="color: rgba(0, 0, 255, 1)">lambda</span><span style="color: rgba(0, 0, 0, 1)"> d: d.find_element_by_xpath(
</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">//*[@id="article-list"]/div/div/div/div/div/a</span><span style="color: rgba(128, 0, 0, 1)">'</span>)).get_attribute(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">href</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
article_id </span>= preview_link.split(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">=</span><span style="color: rgba(128, 0, 0, 1)">'</span>)[-1<span style="color: rgba(0, 0, 0, 1)">]
index_page </span>= WebDriverWait(driver, timeout).until(<span style="color: rgba(0, 0, 255, 1)">lambda</span> d: d.find_element_by_xpath(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">//a[@class="shead_logo"]</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">))
index_page.click()
driver.get(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">https://mp.toutiao.com/profile_v3/index</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(r.scard(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">cookie_pool_toutiao</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">))
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> preview_link, article_id
</span><span style="color: rgba(0, 0, 255, 1)">if</span> <span style="color: rgba(128, 0, 128, 1)">__name__</span> == <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">__main__</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">:
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">start</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
start_time </span>=<span style="color: rgba(0, 0, 0, 1)"> time.time()
title </span>= <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Children</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
content </span>= <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)"><p>cute</p><p><img class="wscnph" src="http://img.mp.itc.cn/upload/20170105/1a7095f0c7eb4316954dda4a8b93b88c_th.jpg" /></p></span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
expand_link </span>= <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">https://www.cnblogs.com/Summer-skr--blog/</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">
img </span>= <span style="color: rgba(128, 0, 0, 1)">''</span><span style="color: rgba(0, 0, 0, 1)">
preview_link, article_id </span>=<span style="color: rgba(0, 0, 0, 1)"> toutiao_save_and_preview(title, content, expand_link)
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(preview_link)
</span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(article_id)
finish_time </span>=<span style="color: rgba(0, 0, 0, 1)"> time.time()
</span><span style="color: rgba(0, 0, 255, 1)">print</span>(finish_time - start_time)</pre>
</div>
<span class="cnblogs_code_collapse">View Code</span></div>
<p> </p>
<p>selenium相关文档:</p>
<p> https://www.seleniumhq.org/docs/</p>
<p> https://selenium-python.readthedocs.io</p>
<p> </p>
<p><strong><span style="font-family: "Microsoft YaHei"; font-size: 18px">哈哈,认认真真的写了这么长博文,如果您觉得对您有帮助,麻烦帮忙点个赞哦!一起加油!</span></strong></p><br><br>
来源:https://www.cnblogs.com/Summer-skr--blog/p/11491078.html
頁:
[1]