当前位置: 首页 > news >正文

网站开发api平台铜山徐州网站开发

网站开发api平台,铜山徐州网站开发,公司营销网站建设,网站制作软件排名文章目录 前期准备1. 浏览器开启远程控制指令#xff08;1#xff09;Edge#xff08;2#xff09;Chrome 2. 执行python代码#xff08;1#xff09;先启动浏览器后执行代码#xff08;2#xff09;通过代码启动浏览器#xff08;3#xff09;Bug问题记录1#xff… 文章目录 前期准备1. 浏览器开启远程控制指令1Edge2Chrome 2. 执行python代码1先启动浏览器后执行代码2通过代码启动浏览器3Bug问题记录1python可读取浏览器所有标签标题但检索网页元素失败2浏览器开启程序但python程序无法链接浏览器进行自动控制 3. 爬取效果3. 完整代码共享3.1 包含Excel部分的完整代码3.2 爬虫部分的完整代码 说明本记录是在Windows系统上执行的 起因是博导要求统计一下国内某个领域的专家情况统计主持国家自然科学基金的副教授和教授都有哪些大牛 于是本人去[NSFC]https://kd.nsfc.cn/ 下载全部的历史基金项目书。。。。工作量太大就……半自动化实现吧 前期准备 1. python Selenium库 2. Edge浏览器 或 Chrome浏览器1. 浏览器开启远程控制指令 无论是哪种浏览器都需要使用终端独立运行浏览器的远程调试模式。开启方式加入指令–remote-debugging-port9222 --user-data-dir“D:\selenium\AutomationProfile” 需要进入目标浏览器的根目录 不然就输入全路径 1Edge .\msedge.exe --remote-debugging-port9222 --user-data-dir“D:\selenium\AutomationProfile”2Chrome .\chrome.exe --remote-debugging-port9222 --user-data-dir“D:\selenium\AutomationProfile”2. 执行python代码 1先启动浏览器后执行代码 必须是先执行上述步骤开启了浏览器的远程调试端口后才能通过下方代码进行控制。 add_experimental_option(debuggerAddress, 127.0.0.1:9222) 这句话是关键 from selenium import webdriver from selenium.webdriver.edge.options import Optionsclass Test:def edge(self):edge_driver_path executable_pathrC:\Program Files (x86)\Microsoft\Edge\Application\msedge.exechrome_options Options()# chrome_options.binary_location edge_driver_path # 传入驱动地址chrome_options.add_experimental_option(debuggerAddress, 127.0.0.1:9222) # 127.0.0.1:9222其中9222是浏览器的运行端口# 让浏览器带着这个配置运行# chrome_options.add_experimental_option(detach, True) # 通过option参数设置浏览器不关闭driver webdriver.Edge(optionschrome_options, keep_aliveTrue)driver.implicitly_wait(10) # 页面元素查找的等待时间self.driver driverpassdef chrome_drive(self, drivechrome):edge_driver_path executable_path rD:\Program Files\Google\Chrome\Applicationif drive chrome:chrome_options webdriver.ChromeOptions()# chrome_options.binary_location edge_driver_path # 传入驱动地址# chrome_options.add_experimental_option(detach, True) # 通过option参数设置浏览器不关闭chrome_options.add_experimental_option(debuggerAddress, 127.0.0.1:9222)driver webdriver.Chrome(optionschrome_options, keep_aliveFalse)driver.implicitly_wait(10) # 页面元素查找的等待时间self.driver driverpass2通过代码启动浏览器 这个时候被注释掉的 .binary_location edge_driver_path 是关键这种情况下需要下载对应的驱动软件.exe博主在笔记本电脑上首次尝试Selenium时就下载了驱动软件但后来在台式电脑使用相同代码时发现压根不需要下载什么驱动软件只需要使用终端提前启动浏览器的调试模型即可。 这是弯路、坑因为如果是通过代码启动浏览器的调试模型需要配置路径然后保证程序关闭后浏览器依旧运行麻烦 3Bug问题记录 1python可读取浏览器所有标签标题但检索网页元素失败 部分网页不支持爬取特别是当网页开启F12的开发人选项后会出现无法查找元素的问题。此时关闭 “开发人选项” 即可。 2浏览器开启程序但python程序无法链接浏览器进行自动控制 关闭原有浏览器重新打开浏览器需搭配命令–remote-debugging-port9222 --user-data-dir“xxx folder” 3. 爬取效果 3. 完整代码共享 以下代码主要实现了 浏览器标签页的翻动和选择爬取 – 青塔网检索”国家自然科学基金项目“的作者信息并保存到表格。爬取 – NSFC”国家自然科学基金项目“的作者信息并保存到表格。爬取 – 国际某个领域专家的作者信息并保存到表格。 3.1 包含Excel部分的完整代码 包含Excel部分的完整代码见资源文件 3.2 爬虫部分的完整代码 import os from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By import time from selenium.webdriver.edge.options import Options from selenium.webdriver.common.action_chains import ActionChains# .\chrome.exe --remote-debugging-port9222 --user-data-dir“D:\selenium\AutomationProfile” n * --ws --allow-insecure-unlock --nodiscover --authrpc.addr 127.0.1.2 --authrpc.port 8545 # .\chrome.exe --remote-debugging-port9222 --user-data-dir“D:\selenium\AutomationProfile”class Web_Browser:def __init__(self, drivechrome):self.driver None# self.edge()self.chrome_drive()def edge(self):# edge_driver_path executable_pathrD:\Program Files\Google\Chrome\Application\chromedriver.exeedge_driver_path executable_pathrC:\Program Files (x86)\Microsoft\Edge\Application\msedge.exechrome_options Options()# chrome_options.binary_location edge_driver_path# 配置浏览器# 添加User-Agent到Chrome选项中# chrome_options.add_argument(--user-agentwindows 10 Edge)# 127.0.0.1:9222其中9222是浏览器的运行端口chrome_options.add_experimental_option(debuggerAddress, 127.0.0.1:9222)# 让浏览器带着这个配置运行# chrome_options.add_experimental_option(detach, True) # 通过option参数设置浏览器不关闭driver webdriver.Edge(optionschrome_options, keep_aliveTrue)# driver webdriver.Chrome( optionschrome_options)print()# driver.get(www.baidu.com)driver.implicitly_wait(10)self.driver driverdef chrome_drive(self, drivechrome):edge_driver_path executable_path rD:\Program Files\Google\Chrome\Application\chromedriver.exeif drive chrome:chrome_options webdriver.ChromeOptions()# chrome_options.binary_location edge_driver_path# chrome_options.add_experimental_option(detach, True) # 通过option参数设置浏览器不关闭chrome_options.add_experimental_option(debuggerAddress, 127.0.0.1:9222)driver webdriver.Chrome(optionschrome_options, keep_aliveFalse)self.driver driverdriver.implicitly_wait(10)self.opened_windows_dict Nonepassdef get_all_opened_windows(self):driver self.drivercw driver.current_window_handleres {}# 获取已打开的标签页的信息tabs driver.window_handlesfor t in tabs:driver.switch_to.window(t)res[str(driver.title)] str(t)self.opened_windows_dict resdriver.switch_to.window(cw)print(已打开的标签页的信息:,)for k in res: print(f\t{k}: {res[k]})return resdef switch_window(self, key):driver self.drivercw driver.current_window_handle# 获取已打开的标签页的信息tabs driver.window_handlesfor t in tabs:driver.switch_to.window(t)if key in str(driver.title): cw tbreak# driver.switch_to.window(cw)self.driver driverpassdef open_new_window(self, driverNone, urlNone, delay_t0.6):# 打开新标签页driver self.driver if not driver else driverold_handle driver.window_handles # 获取已打开的标签页的信息# driver.find_element(body).send_keys(Keys.CONTROL t) # 没有实体会报错# driver.execute_script(window.open(,_blank);) # 可能被拦截driver.switch_to.new_window(tab)time.sleep(delay_t)if len(driver.window_handles) len(old_handle): return Truedriver.execute_script(fwindow.open({url if url else });)time.sleep(delay_t)if len(driver.window_handles) len(old_handle): return Truereturn Falsedef func1(self, xlsx): 学术网 for p in range(50):# self.switch_window(故障诊断)driver self.driverweb driver.find_element(byBy.XPATH, value//*[idsearch_body]/div[2]/div[3]/div[1]/div[2]/div[1]/div[3]/div[2]/div/div[2]/div[2]/div/div)web1 web.find_elements(byBy.CLASS_NAME, valueinner-content)print(web1 len, len(web1))num 0for i, w in enumerate(web1):try:# //*[idsearch_body]/div[2]/div[3]/div[1]/div[2]/div[1]/div[3]/div[2]/div/div[2]/div[2]/div/div#a w.find_element(byBy.XPATH, valuef//div[{1i}]/div/div[2]/div[1]/div[1]/div/a/strong/span/span).texttry:b w.find_element(byBy.XPATH, valuef//div[{1 i}]/div/div[2]/div[3]/p[2]).textschool str(b).split(,)for s in school:if university in s.lower(): b s[1:]except: b Nonec w.find_element(byBy.XPATH, valuef//div[{1 i}]/div/div[2]/div[3]/p[1]).textd Nonee Nonef Nonetry:h_index w.find_element(byBy.XPATH, valuef//div[{1 i}]/div/div[2]/div[2]/div/span[1]/span[3]).textpaper w.find_element(byBy.XPATH, valuef//div[{1 i}]/div/div[2]/div[2]/div/span[2]/span[3]).textcite w.find_element(byBy.XPATH, valuef//div[{1 i}]/div/div[2]/div[2]/div/span[3]/span[3]).textf fH-index: {h_index}, papers: {paper}, cites: {cite}except: passg Noneh w.find_element(byBy.XPATH, valuef//div[{1 i}]/div/div[2]/div[1]/div[1]/div/a)h https://www.aminer.cn/ h.get_attribute(href)print(a, b ,c, g)xlsx.input_data(a,b,c,d,e,f,g, h)num 1except: passprint(记录, num)# aa driver.find_elements(byBy.XPATH, value//*[idsearch_body]/div[2]/div[3]/div[1]/div[2]/div[1]/div[3]/div[2]/div/div[2]/div[3]/ul/li)# aa aa[-1]aa driver.find_element(byBy.CLASS_NAME, valueant-pagination-next)# v #search_body div.ant-tabs.ant-tabs-top.a-aminer-core-search-index-searchPageTab.ant-tabs-line.ant-tabs-no-animation div.ant-tabs-content.ant-tabs-content-no-animated.ant-tabs-top-content div.ant-tabs-tabpane.ant-tabs-tabpane-active div.a-aminer-core-search-index-componentContent div.a-aminer-core-search-c-search-component-temp-searchComponent div.view div:nth-child(2) div div:nth-child(2) div.paginationWrap ul li.ant-pagination-next# aa driver.find_element(byBy.CSS_SELECTOR, valuev)# 创建一个ActionChains对象用于执行鼠标动作action_chains ActionChains(driver)# 将鼠标移动到链接元素上并点击action_chains.move_to_element(aa).click().perform()print(f第{p1}页 -- 第{p2}页)try:xlsx.make_frame()xlsx.save_excel()except: passtime.sleep(5)passdef func2(self, xlsxNone):for p in range(50):self.switch_window(青塔)driver self.driverweb driver.find_element(byBy.XPATH,value//*[idapp]/div[2]/div[1]/div/div[2]/div[2]/div/div[2])web1 web.find_elements(byBy.CLASS_NAME, valuelist-item)print(web1 len, len(web1))num 0for i, w in enumerate(web1):# try:# //*[idapp]/div[2]/div[1]/div/div[2]/div[2]/div/div[2]# //*[idapp]/div[2]/div[1]/div/div[2]/div[2]/div/div[2]/div/div[2]/div[2]/div[2]/div[1]/div[2]# //*[idapp]/div[2]/div[1]/div/div[2]/div[2]/div/div[2]/div/div[1]/div[2]/div[2]/div[1]/div[1]b w.find_element(byBy.XPATH, valuef//div[2]/div[1]/div[1]/div[2])print(b)b b.textprint(b, b)a w.find_element(byBy.XPATH, valuef//div[2]/div[2]/div[1]/div[2]).textprint(a, a)c Noned Nonee w.find_element(byBy.XPATH, valuef//div[1]/div[1]).textprint(e, e)year w.find_element(byBy.XPATH, valuef//div[2]/div[2]/div[2]/div[2]).textmoney w.find_element(byBy.XPATH, valuef//div[2]/div[1]/div[2]/div[2]).textprint(year, year, money, money)e f{e}, 立项: {year}, 资助: {money}jijin w.find_element(byBy.XPATH, valuef//div[2]/div[3]/div[1]/div[2]).textdomain w.find_element(byBy.XPATH, valuef//div[2]/div[3]/div[2]/div[2]).textprint(jijin,jijin, domain, domain)f f{jijin}, 领域: {domain}g Noneh Noneprint(i, -----------, i)print(a, b, c, d, e, f)xlsx.input_data(a, b, c, d, e, f, g, h)num 1break# except: passprint(记录, num)breakaa driver.find_element(byBy.XPATH, valuef//*[idapp]/div[2]/div[1]/div/div[2]/div[2]/div/div[3]/button[2])# 创建一个ActionChains对象用于执行鼠标动作action_chains ActionChains(driver)# 将鼠标移动到链接元素上并点击action_chains.move_to_element(aa).click().perform()print(f第{p 1}页 -- 第{p 2}页)try:xlsx.make_frame()xlsx.save_excel()except:passtime.sleep(5)passdef func3(self, xlsxNone):for p in range(50):self.switch_window(大数据知识管理服务门户)driver self.driverd driver.find_element(byBy.CLASS_NAME, valuecontainer_list_right)print(d, d)# web driver.find_element(byBy.XPATH,# value//*[idapp]/div[1]/div[3]/div/div[3]/div[1]/div)web d.find_element(byBy.XPATH, value//div[1]/div)# web1 web.find_elements(byBy.CLASS_NAME, valuelist-item)# print(web1 len, len(web1))num 0for i, w2 in enumerate(range(6)):w webtry:# //*[idapp]/div[1]/div[3]/div/div[3]/div[1]/div# //*[idapp]/div[1]/div[3]/div/div[3]# //*[idapp]/div[1]/div[3]/div/div[3]/div[1]/div/div[2]/div[2]/div[1]b w.find_element(byBy.XPATH, valuef//div[{i1}]/div[3]/div[4]/a)b b.text# print(b, b)a w.find_element(byBy.XPATH, valuef//div[{i1}]/div[2]/div[4]/a).text# print(a, a)c Noned Nonee w.find_element(byBy.XPATH, valuef//div[{i1}]/div[1]/div[1]/p/a).text# print(e, e)year w.find_element(byBy.XPATH, valuef//div[{i1}]/div[3]/div[3]).textmoney w.find_element(byBy.XPATH, valuef//div[{i1}]/div[3]/div[1]).text# print(year, year, money, money)e f{e}, {year}, {money}jijin w.find_element(byBy.XPATH, valuef//div[{i1}]/div[2]/div[3]).textdomain w.find_element(byBy.XPATH, valuef//div[{i1}]/div[2]/div[1]).text# print(jijin,jijin, domain)f f{jijin}, {domain}g Noneh Noneprint(i1, -----------, i1)print(a, b, c, d, e, f)xlsx.input_data(a, b, c, d, e, f, g, h)num 1# breakexcept: passprint(记录, num)# break# aa driver.find_element(byBy.CLASS_NAME, valuefbtn-next)# # 创建一个ActionChains对象用于执行鼠标动作# action_chains ActionChains(driver)# # 将鼠标移动到链接元素上并点击# action_chains.move_to_element(aa).click().perform()print(f第{p 1}页 -- 第{p 2}页)try:xlsx.make_frame()xlsx.save_excel()except:passbreak# time.sleep(5)passdef func4(self, xlsxNone, keyGoogle2):if key Google: self.switch_window(Google)else: self.switch_window(必应)driver self.driverdata xlsx.read_excel()# print(data[姓名])for i, name in enumerate(data[姓名]):school data[学校][i]text f{school}{name}是不是教授print(fsearch [{i1}]: {name} -》 , text)if key Google: web driver.find_element(byBy.XPATH, value//*[idAPjFqb])else: web driver.find_element(byBy.XPATH, value//*[idsb_form_q])web.clear()web.send_keys(text)if key Google: web driver.find_element(byBy.XPATH, value//*[idtsf]/div[1]/div[1]/div[2]/button)else: web driver.find_element(byBy.XPATH, value//*[idsb_form_go])# try:web.click()# except: passtime.sleep(5)num 0if __name__ __main__:from temp import Make_Excel, input_data_list, input_dataxlsx Make_Excel()web Web_Browser()web.get_all_opened_windows()# web.switch_window(故障诊断) 学术网 web.func1(xlsx) # 学术网# web.func2(xlsx) # 青塔网# web.func3(xlsx) # NSFC官网# web.func4(xlsx, ) # goole搜索网# xlsx.make_frame()# xlsx.save_excel()pass
http://www.hkea.cn/news/14414283/

相关文章:

  • 做饲料机械的网站带有flash的网站
  • 整站seo优化公司有wordpress模板安装教程
  • 衡水做wap网站server 2008 网站部署
  • 嘉兴专业定制网站制作企业在线网页代理搭建
  • 企业网络营销站点的功能有哪些如何设计大气的网站
  • apicloud开发教程电脑优化工具
  • 罗庄建设局网站没有经验
  • 网站建设网上售票系统优设网址导航属于网络导航吗
  • 湖北省工程建设协会网站手机网络营销方案
  • js 取网站域名没有收款接口网站怎么做收款
  • 设计一个电子商务网站wordpress主题怎么删除边栏
  • dw做电影网站wordpress使用腾讯cos
  • 不记得在哪里做的网站备案大丰做网站哪家公司好
  • 卖汽车配件怎么做网站广州珠吉网站建设
  • 乐达网站建设万网官网域名
  • 网站后台功能模块设计wordpress windows 权限
  • 杭州商城网站制作网络营销策略分哪几类
  • 怎么做示爱的网站外国做ppt的网站
  • 网站 建设需求wordpress适合视频网站吗
  • 四个字网站 域名常州网站推广公司哪家好
  • 网络网站建设10大指标张家港做网站费用
  • 个人网站建设需求说明书做条形图的网站
  • 什么是网站建设方案书做电商网站用什么语言
  • 手机网站建设方案doc建设网站产品图片显示不全
  • 网站首页版式上海模板建站哪家好
  • 嘉定建设厅网站有机蔬菜网站是如何建设
  • 甘肃省建设厅网站资质升级公示wordpress 后台移除新闻
  • 怎么办网站手机网页微信登录入口
  • 网站建设及运营服务流程群晖搭建的wordpress外网访问
  • 我想做个网站做导航网站电脑设备