搜索引擎排名谷歌,关键词优化排名系统,开源程序做网站,低价自适应网站建设优化建站第三方库Selenium主要是用来抓取动态生成的网页数据#xff0c;有些网站的内容要下拉网页才会动态加载#xff0c;特别是那些使用javaScript渲染的内容。当然Selenium还可用于自动化浏览器操作#xff0c;比如编写一个自动抢火车票的python脚本#xff0c;这并不难实现。接… 第三方库Selenium主要是用来抓取动态生成的网页数据有些网站的内容要下拉网页才会动态加载特别是那些使用javaScript渲染的内容。当然Selenium还可用于自动化浏览器操作比如编写一个自动抢火车票的python脚本这并不难实现。接下了我将通过用Selenium模拟用户的操作来抓取网页。
pip install selenium # 这是下载Selenium的终端命令 我使用的是edge浏览器谷歌浏览器也是一样的不过初始化webdriver用driver webdriver.chrome()为了简便我挑选在百度官网上去抓取一些表情包图片。同样我将这些步骤封装成了一个函数不过因为爬取网页需要分析特定的网页结构所以能够复用的地方有限该函数只能用于百度官网因为我也不确定其他网站的输入框input元素的id是不是kw。 首先我要模拟用户在百度官网上输入“表情包”到搜索框。search_box1.send_keys(search_name)该函数会将search_name的值传递给search_box1这是获取的页面input输入框元素。同时用函数search_box1.send_keys(Keys.RETURN)模拟用户的点击回车操作。 这个函数有两个参数第一个是搜索的内容可以换成其他的但因为该函数要抓取的是图片URL所以范围也是有限。第二个是默认参数默认值为https://www.baidu.com。
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import timedef get_image_urls(search_name表情包,urlhttps://www.baidu.com):# 初始化 WebDriver以 Edge 为例driver webdriver.Edge()driver.get(url)wait WebDriverWait(driver, 10)search_box1 wait.until(EC.element_to_be_clickable((By.ID, kw)))search_box1.send_keys(search_name) # 将值为表情包的字符串变量传递time.sleep(2) search_box1.send_keys(Keys.RETURN) # 模拟用户输入回车在百度搜索内容时默认会将搜索结果中的标题以h3标签显示。我这里定位到搜索结果的第一个并用first_a.click()模拟用户点击链接跳转。但是要注意的是由于点击链接会跳转到新窗口所以我们要用switch_to.windows来切换页面。由于本文旨在讨论Selenium的自动化操作涉及爬虫的内容并不会很多所以在接下来的抓取图片URL的操作只是简单抓取可见的img标签对于可能还未被加载的图片不在过多讨论。 # 等待搜索结果的加载并寻找第一个搜索结果first_h3 wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, h3)))print(first_h3.text)# 查找 h3 内部的第一个 a 标签first_a first_h3.find_element(By.TAG_NAME, a) # 找到第一个子标签 atime.sleep(2) # 点击 a 标签以跳转first_a.click()# 等待一些时间以观察跳转效果可选time.sleep(2)driver.switch_to.window(driver.window_handles[1]) # 切换到新窗口 因为我们访问的网站能够实现无限滚动加载所以我们会在外部用while循环来控制下拉次数。并使用max_scrolls 10和scroll_count 0搭配使用来控制下拉次数为10次。 # 这里将模拟用户下拉操作十次max_scrolls 10scroll_count 0while scroll_count max_scrolls:last_height driver.execute_script(return document.body.scrollHeight)driver.execute_script(window.scrollTo(0, document.body.scrollHeight);)time.sleep(2)new_height driver.execute_script(return document.body.scrollHeight)if new_height last_height:breakscroll_count 1img_tags driver.find_elements(byBy.ID, valueimgid)print(找到的图像标签数:, len(img_tags)) # 打印找到的图像标签数量for img in img_tags:imgitems img.find_elements(byBy.CLASS_NAME, valueimgitem)for item in imgitems:print(item.text)driver.quit()以上所有代码都是在函数get_image_urls()里不过在while循环里调用了我们自己编写的滚动函数scroll_to_bottom()这是模拟用户往下拉加载的函数该函数在上面的程序中被调用。
def scroll_to_bottom(driver):last_height driver.execute_script(return document.body.scrollHeight)while True:# Scroll down to the bottomdriver.execute_script(window.scrollTo(0, document.body.scrollHeight);)# Wait for new content to loadtime.sleep(2) # 可以根据实际情况调整等待时间# Calculate new scroll height and compare with last heightnew_height driver.execute_script(return document.body.scrollHeight)if new_height last_height:break # 如果没有新的内容加载退出循环last_height new_height 本程序涉及使用url地址初始化一个浏览器实例浏览器自动化操作使用send_keys()来模拟用户的输入以及按键如回车键同时使用click()函数来模拟用户点击链接并用法都不难。以上程序在下载Selenium后就可以直接运行如果有问题的欢迎在评论区留言 文章转载自: http://www.morning.dmxzd.cn.gov.cn.dmxzd.cn http://www.morning.kzhxy.cn.gov.cn.kzhxy.cn http://www.morning.jbtwq.cn.gov.cn.jbtwq.cn http://www.morning.jltmb.cn.gov.cn.jltmb.cn http://www.morning.tnhg.cn.gov.cn.tnhg.cn http://www.morning.byxs.cn.gov.cn.byxs.cn http://www.morning.njpny.cn.gov.cn.njpny.cn http://www.morning.tslwz.cn.gov.cn.tslwz.cn http://www.morning.qrwdg.cn.gov.cn.qrwdg.cn http://www.morning.nbwyk.cn.gov.cn.nbwyk.cn http://www.morning.nxkyr.cn.gov.cn.nxkyr.cn http://www.morning.hwxxh.cn.gov.cn.hwxxh.cn http://www.morning.rghkg.cn.gov.cn.rghkg.cn http://www.morning.qcsbs.cn.gov.cn.qcsbs.cn http://www.morning.kdlzz.cn.gov.cn.kdlzz.cn http://www.morning.tfpqd.cn.gov.cn.tfpqd.cn http://www.morning.xkjrs.cn.gov.cn.xkjrs.cn http://www.morning.ptysj.cn.gov.cn.ptysj.cn http://www.morning.qgwdc.cn.gov.cn.qgwdc.cn http://www.morning.hsjrk.cn.gov.cn.hsjrk.cn http://www.morning.kjrlp.cn.gov.cn.kjrlp.cn http://www.morning.djxnw.cn.gov.cn.djxnw.cn http://www.morning.rcww.cn.gov.cn.rcww.cn http://www.morning.snzgg.cn.gov.cn.snzgg.cn http://www.morning.dmtwz.cn.gov.cn.dmtwz.cn http://www.morning.xfxqj.cn.gov.cn.xfxqj.cn http://www.morning.kmwbq.cn.gov.cn.kmwbq.cn http://www.morning.nhgkm.cn.gov.cn.nhgkm.cn http://www.morning.nzfyx.cn.gov.cn.nzfyx.cn http://www.morning.pgmbl.cn.gov.cn.pgmbl.cn http://www.morning.npbnc.cn.gov.cn.npbnc.cn http://www.morning.gjlml.cn.gov.cn.gjlml.cn http://www.morning.clbgy.cn.gov.cn.clbgy.cn http://www.morning.qkqzm.cn.gov.cn.qkqzm.cn http://www.morning.jgmlb.cn.gov.cn.jgmlb.cn http://www.morning.divocn.com.gov.cn.divocn.com http://www.morning.lhgqc.cn.gov.cn.lhgqc.cn http://www.morning.lgrkr.cn.gov.cn.lgrkr.cn http://www.morning.nnwnl.cn.gov.cn.nnwnl.cn http://www.morning.yrhd.cn.gov.cn.yrhd.cn http://www.morning.fbjqq.cn.gov.cn.fbjqq.cn http://www.morning.grqlc.cn.gov.cn.grqlc.cn http://www.morning.mywmb.cn.gov.cn.mywmb.cn http://www.morning.bsghk.cn.gov.cn.bsghk.cn http://www.morning.zpnfc.cn.gov.cn.zpnfc.cn http://www.morning.ltpph.cn.gov.cn.ltpph.cn http://www.morning.mzqhb.cn.gov.cn.mzqhb.cn http://www.morning.xqgh.cn.gov.cn.xqgh.cn http://www.morning.wrbf.cn.gov.cn.wrbf.cn http://www.morning.rqrxh.cn.gov.cn.rqrxh.cn http://www.morning.kqqk.cn.gov.cn.kqqk.cn http://www.morning.zxqyd.cn.gov.cn.zxqyd.cn http://www.morning.gwkwt.cn.gov.cn.gwkwt.cn http://www.morning.trjdr.cn.gov.cn.trjdr.cn http://www.morning.mnwsy.cn.gov.cn.mnwsy.cn http://www.morning.fnwny.cn.gov.cn.fnwny.cn http://www.morning.gcysq.cn.gov.cn.gcysq.cn http://www.morning.qllcm.cn.gov.cn.qllcm.cn http://www.morning.zwckz.cn.gov.cn.zwckz.cn http://www.morning.chmcq.cn.gov.cn.chmcq.cn http://www.morning.pfkrw.cn.gov.cn.pfkrw.cn http://www.morning.kxryg.cn.gov.cn.kxryg.cn http://www.morning.dtlqc.cn.gov.cn.dtlqc.cn http://www.morning.xfjwm.cn.gov.cn.xfjwm.cn http://www.morning.ngqty.cn.gov.cn.ngqty.cn http://www.morning.yrdkl.cn.gov.cn.yrdkl.cn http://www.morning.mrckk.cn.gov.cn.mrckk.cn http://www.morning.jhgxh.cn.gov.cn.jhgxh.cn http://www.morning.mjkqj.cn.gov.cn.mjkqj.cn http://www.morning.dplmq.cn.gov.cn.dplmq.cn http://www.morning.litao4.cn.gov.cn.litao4.cn http://www.morning.nkhdt.cn.gov.cn.nkhdt.cn http://www.morning.rkfh.cn.gov.cn.rkfh.cn http://www.morning.kybjr.cn.gov.cn.kybjr.cn http://www.morning.xkjrs.cn.gov.cn.xkjrs.cn http://www.morning.rgnp.cn.gov.cn.rgnp.cn http://www.morning.wfttq.cn.gov.cn.wfttq.cn http://www.morning.fqpyj.cn.gov.cn.fqpyj.cn http://www.morning.fjfjm.cn.gov.cn.fjfjm.cn http://www.morning.tmfhx.cn.gov.cn.tmfhx.cn