0


【日常】爬虫进阶技巧:selenium加载扩展插件(extension)与配置用户数据(user-data)

文章目录


序言

本文将简单阐述关于

  1. Python
  2. \text{Python}
  3. Python使用浏览器驱动(
  4. Selenium
  5. \text{Selenium}
  6. Selenium)中的配置(
  7. options
  8. \text{options}
  9. options)进阶性技巧:
  1. from selenium import webdriver
  2. chrome_options = webdriver.ChromeOptions()# chrome_options.add_extension(...)# chrome_options.add_argument(...)# chrome_options.add_experimental_option(...)

最全的配置信息可以参考

  1. Chrome
  2. \text{Chrome}
  3. Chrome官网,如果无法翻墙,也可以搜到比较全的博客(如CSDN@Kosmoo),笔者不会介绍很多的配置选项,仅就配置插件与配置用户数据两点,结合实例进行阐述。

笔者撰写与爬虫相关的博客通常会围绕一个完整的爬虫任务流程进行梳理,极少选取这种技巧性的问题点来小题大做创作一篇博客。然而学习爬虫技巧并无捷径可行,只有在若干实践中不断发现障碍并设法解决后,你的经验才会不断提升,在以后遇到类似的瓶颈才能迎刃而解。

也许在阅读本文的你已经是能够熟练应用

  1. Selenium
  2. \text{Selenium}
  3. Selenium
  4. JS
  5. \text{JS}
  6. JS逆向的方法的老手,认为在浏览器中所阅所闻都可以轻而易举地爬取得到,但是笔者确信你在爬虫过程中仍然会遇到令人费解的问题,因为总是会有你不熟悉的知识。**比如在相同的操作流程下,
  7. Selenium
  8. \text{Selenium}
  9. Selenium驱动与实际浏览器操作看到的页面并不相同**,在这种情况下,笔者就会非常高兴,因为问题出现将意味着爬虫技能的提升,如果始终没有问题出现,只会说明潜在的问题越来越多,爬虫本身也便是失去了乐趣。

序言的最后,笔者将介绍本文的爬虫任务。近期组内正在准备筹办

  1. 2021
  2. 2021
  3. 2021年的国际会议,老板让我们去
  4. Engineering Village
  5. \text{Engineering Village}
  6. Engineering Village通过搜索各个会议名称得到若干论文链接,从中论文链接中可以获取论文作者的姓名与邮箱信息,用于发布会议征文邮件:
    1. Figure 1 \text{Figure 1} Figure 1 Engineering Village \text{Engineering Village} Engineering Village搜索页面

请添加图片描述

由于笔者所在的高校没有访问

  1. Engineering Village
  2. \text{Engineering Village}
  3. Engineering Village数据库的权限,好在老板给了学霸图书馆的
  4. VIP
  5. \text{VIP}
  6. VIP(相当于借用其他高校的统一身份认证信息去访问
  7. Engineering Village
  8. \text{Engineering Village}
  9. Engineering Village,类似
  10. VPN
  11. \text{VPN}
  12. VPN)。然而校园网访问
  13. Engineering Village
  14. \text{Engineering Village}
  15. Engineering Village的效率实在是过于迟缓,而且经常性地会发生页面崩溃的情况,这使得人工完成收集姓名与邮箱的任务并不容易。

事实上去年在筹办会议时笔者已经写好了爬虫逻辑,虽然今年

  1. Engineering Village
  2. \text{Engineering Village}
  3. Engineering Village的页面完全没有发生更新,去年的爬虫逻辑依然可行,但是今年学霸图书馆提供的高校通道发生了更新,需要**下载插件**才能借用其他高校的统一身份认证信息去访问
  4. Engineering Village
  5. \text{Engineering Village}
  6. Engineering Village
    1. Figure 2 \text{Figure 2} Figure 2:插件安装说明。注意该插件只能在 Chrome \text{Chrome} Chrome内核的浏览器中安装,火狐浏览器与 Edge \text{Edge} Edge一定无法安装,因此本文的 S e l e n i u m \rm Selenium Selenium要求必须在 Chrome \text{Chrome} Chrome上启用,需要先行安装 Chromedriver \text{Chromedriver} Chromedriver,具体方法详见https://www.cnblogs.com/lfri/p/10542797.html

请添加图片描述
对本文的爬虫任务并不感兴趣的朋友,可以直接跳过接下来的序言内容直接阅读正文部分。

如果对本文所述的爬虫任务感兴趣的朋友,可以去学霸图书馆申请注册一个新账号,登录账号后访问Welcome页面可以获得免费的

  1. 24
  2. 24
  3. 24小时
  4. VIP
  5. \text{VIP}
  6. VIP体验,这将有利于你对接下来本文爬虫代码的使用。重新登陆账号后进入**数据库大全**,在**外文数据库大全**中找到
  7. Engineering Village EI
  8. \text{Engineering Village EI}
  9. Engineering Village EI工程链接进入:
    1. Figure 3 \text{Figure 3} Figure 3:找到 Engineering Village EI \text{Engineering Village EI} Engineering Village EI工程数据库链接

请添加图片描述
即可找到以下两个通道,注意这两个通道可能过一段时间就不存在了,去年笔者使用的是沈阳工业大学的通道,今年的两个分别是中科大与北航的通道,虽然以后通道可能也会发生变化,但是

  1. Engineering Village
  2. \text{Engineering Village}
  3. Engineering Village的爬虫逻辑通常是不会变的,因此本文爬虫的维护范围仅限于进入通道:
    1. Figure 4 \text{Figure 4} Figure 4:找到高校通道链接

请添加图片描述
点击上图中任意一个通道链接即发现上文中所提及的插件:

    1. Figure 5 \text{Figure 5} Figure 5 2021 2021 2021更新内容,需要安装插件访问通道,该插件的安装并不复杂,按照教程选择第二种 zip \text{zip} zip格式的在 Chrome \text{Chrome} Chrome中手动安装解压包,之后弃用可以直接彻底删除。

请添加图片描述
本文的问题从这里开始。


  1. 1 Selenium
  2. \text{1 Selenium}
  3. 1 Selenium加载插件(
  4. Chromedriver
  5. \text{Chromedriver}
  6. Chromedriver

  1. 1
  2. 1
  3. 1点比较浅显。

即便你根据

  1. Figure 2
  2. \text{Figure 2}
  3. Figure 2
  4. Chrome
  5. \text{Chrome}
  6. Chrome上安装好插件后,使用
  7. Chrome
  8. \text{Chrome}
  9. Chrome
  10. Selenium
  11. \text{Selenium}
  12. Selenium驱动访问通道时依然会卡在
  13. Figure 5
  14. \text{Figure 5}
  15. Figure 5的提示页面上,这是一件很
  16. tricky
  17. \text{tricky}
  18. tricky的事情。

笔者不敢确信地说

  1. Selenium
  2. \text{Selenium}
  3. Selenium默认不会加载该插件,因为
  4. Figure 2
  5. \text{Figure 2}
  6. Figure 2中事实上提供了两种插件安装方法,但是第一种(
  7. crx
  8. \text{crx}
  9. crx文件)似乎无法在
  10. Chrome
  11. \text{Chrome}
  12. Chrome中安装,因此只能使用第二种(
  13. zip
  14. \text{zip}
  15. zip文件)安装方法,但是在本文第
  16. 3
  17. 3
  18. 3部分源码中
  19. 81-84
  20. \text{81-84}
  21. 81-84行依然是可以为浏览器驱动配置安装
  22. crx
  23. \text{crx}
  24. crx的插件的。
  1. chrome_options = webdriver.ChromeOptions()# 初始化Chrome选项
  2. chrome_options.add_extension(self.extension_path)# 安装学霸图书馆通道插件
  3. chrome_options.add_argument(r'user-data-dir=C:\Users\lenovo\AppData\Local\Google\Chrome\User Data')
  4. chrome_options.add_experimental_option('useAutomationExtension',False)

这是很有迷惑性的点,因为浏览器驱动(

  1. Selenium
  2. \text{Selenium}
  3. Selenium)中的配置(
  4. options
  5. \text{options}
  6. options)有一项是
  1. --disable-plugins

,即禁用插件,看起来似乎如果不配置

  1. --disable-plugins

参数,浏览器上已经安装的插件是会默认生效的。但是实践结果并非如此,即便不配置

  1. --disable-plugins

参数,插件也不会默认启用,或者可能插件(

  1. plugins
  2. \text{plugins}
  3. plugins)与扩展(
  4. extensions
  5. \text{extensions}
  6. extensions)并不是指同一样事物?

另外无法使用

  1. add_extension

方法安装

  1. zip
  2. \text{zip}
  3. zip格式的插件,虽然在
  4. Chrome
  5. \text{Chrome}
  6. Chrome的扩展安装页面中开启**开发者模式**时可以安装解压的
  7. zip
  8. \text{zip}
  9. zip文件。

总之省略第

  1. 3
  2. 3
  3. 3部分源码中的第
  4. 82
  5. 82
  6. 82行将无法跳过
  7. Figure 5
  8. \text{Figure 5}
  9. Figure 5页面,关于浏览器插件的问题仍然有待考察。

  1. 2 Selenium
  2. \text{2 Selenium}
  3. 2 Selenium配置
  4. User Data
  5. \text{User Data}
  6. User Data的方法与用途(
  7. Chromedriver
  8. \text{Chromedriver}
  9. Chromedriver

这是本次爬虫最令笔者费解的点。

事实上不用走

  1. Figure 4
  2. \text{Figure 4}
  3. Figure 4所示的通道也是有办法进入
  4. Engineering Village
  5. \text{Engineering Village}
  6. Engineering Village的,只要在登陆学霸图书馆后直接在地址栏中输入访问http://www.engineeringvillage.com即可得到
  7. Figure 1
  8. \text{Figure 1}
  9. Figure 1的页面(此时已经获得访问
  10. Engineering Village
  11. \text{Engineering Village}
  12. Engineering Village数据库的权限,可以进行搜索查询),这种方法其实会更加稳定。

但是笔者在使用

  1. Selenium
  2. \text{Selenium}
  3. Selenium驱动时,登陆学霸图书馆后直接在地址栏中输入访问http://www.engineeringvillage.com却始终只能停留在
  4. Welcome
  5. \text{Welcome}
  6. Welcome页面上,即仍然需要登陆验证权限。
    1. Figure 6 \text{Figure 6} Figure 6:无法进入 Figure 1 \text{Figure 1} Figure 1所示的页面(停留在欢迎界面上)

请添加图片描述
这个问题困扰了笔者很长时间,最终只能屈服于走

  1. Figure 4
  2. \text{Figure 4}
  3. Figure 4的通道,但是
  4. Figure 4
  5. \text{Figure 4}
  6. Figure 4中给出的
  7. BUAA
  8. \text{BUAA}
  9. BUAA通道频繁的崩溃,甚至昨晚一夜到今早都没有修复,笔者确信这不是因为插件的问题,于是今早开始测试不同的
  10. options
  11. \text{options}
  12. options参数,想知道到底是哪个参数致使这两者的区别。

最终发现在第

  1. 3
  2. 3
  3. 3部分源码中的第
  4. 83
  5. 83
  6. 83行,即:
  1. chrome_options.add_argument(r'user-data-dir=C:\Users\lenovo\AppData\Local\Google\Chrome\User Data')

在添加了

  1. user-data-dir

参数后,问题迎刃而解。

事实上这个参数将非常有用,许多历史记录、

  1. C
  2. o
  3. o
  4. k
  5. i
  6. e
  7. \rm Cookie
  8. Cookie信息、网站登陆信息都保存在
  9. User Data
  10. \text{User Data}
  11. User Data文件夹中。如一些网站支持浏览器记住登陆信息,如果不添加
  12. User Data
  13. \text{User Data}
  14. User Data参数,这些被记住的信息是无法在
  15. Selenium
  16. \text{Selenium}
  17. Selenium驱动的浏览器中生效的,如果添加该参数,即可轻松的跳过很多网站的登陆验证,而可以免于复杂的验证码处理流程。

  1. 3
  2. 3
  3. 3 源码与运行说明

源码与

  1. crx
  2. \text{crx}
  3. crx插件以及示例的关键词文本笔者已经上传至:
  1. 链接: https://pan.baidu.com/s/1F2AGagKI89Lqi2_leeo5gw
  2. 提取码: hm4q

最后笔者提供本爬虫的完整代码。

仅需修改

  1. xuebalib.py
  2. \text{xuebalib.py}
  3. xuebalib.py中的
  4. 261
  5. ,
  6. 262
  7. ,
  8. 265
  9. 261,262,265
  10. 261,262,265行的用户名、密码及
  11. crx
  12. \text{crx}
  13. crx插件路径即可运行。

另外你需要检查

  1. 83
  2. 83
  3. 83行中的
  4. User Data
  5. \text{User Data}
  6. User Data路径是否与你计算机上的路径吻合,一般来说
  7. Windows
  8. \text{Windows}
  9. Windows系统中
  10. Chrome
  11. \text{Chrome}
  12. Chrome的用户数据路径都是
  1. C:\Users\lenovo\AppData\Local\Google\Chrome\User Data

,但是你最好重新确认一次。

代码格式已经做了优化,注释详细且控制台输出信息可读性较强。

  1. # -*- coding: UTF-8 -*-# @author: caoyang# @email: caoyang@163.sufe.edu.cnimport time
  2. from bs4 import BeautifulSoup
  3. from selenium import webdriver
  4. from selenium.webdriver.support.ui import WebDriverWait
  5. # 读取存放搜索关键词的文件# 文件中的每一行只记录一条关键词# 以'#'开头的行会被自动忽略, 尽量不要出现空行defload_keywords(filepath:str)->list:withopen(filepath,'r', encoding='utf8')as f:
  6. keywords = f.read().splitlines()returnlist(filter(lambda x:not x.startswith('#'), keywords))classXuebalib(object):def__init__(self,
  7. keywords:list,
  8. username:str,
  9. password:str,
  10. host:str,
  11. extension_path:str=None,
  12. mode:str='expert')->None:
  13. self.keywords = keywords[:]
  14. self.username = username
  15. self.password = password
  16. self.host = host
  17. self.extension_path = extension_path # 与2020年的情况出现重大区别, 2021年学霸图书馆通道进入需要安装插件, 需下载crx格式的插件并记录路径
  18. self.mode = mode.strip().lower()assert self.mode in['quick','expert'],f'Unknown mode: {self.mode}'
  19. self.max_trial_time =8@staticmethoddefget_detailurl(soup: BeautifulSoup)->list:"""
  20. 获取索结果页面上所有论文详情页面的链接:
  21. :param soup: 经过BeautifulSoup解析后的页面源代码;
  22. """
  23. detaillinks = soup.find_all('a', class_='detaillink')
  24. detailurls =[]for detaillink in detaillinks:
  25. detailurl = detaillink.attrs['href']
  26. detailurls.append(detailurl)return detailurls
  27. @staticmethoddefget_author_and_email(soup: BeautifulSoup, ignore:bool=False)->list:"""获取论文详情页面上的作者及对应的邮箱
  28. :param soup : 经过BeautifulSoup解析后的页面源代码;
  29. :param ignore : 是否忽略那些没有邮箱的作者, 默认不忽略;
  30. :return author_and_email: 作者和邮箱构成的二元组列表;
  31. """
  32. author_and_email =[]if ignore:
  33. emaillinks = soup.find_all('a', class_='emaillink')for emaillink in emaillinks:
  34. email = emaillink.attrs['href']
  35. authorlink = emaillink.find_previous_sibling('a', class_='authorSearchLink')
  36. author =str(authorlink.string)
  37. author_and_email.append((author, email))else:
  38. ul = soup.find('ul', class_='abs_authors')if ul isnotNone:for li in ul.find_all('li'):
  39. authorlink = li.find('a', class_='authorSearchLink')
  40. emaillink = li.find('a', class_='emaillink')
  41. author =str(authorlink.string)
  42. email =Noneif emaillink isNoneelse emaillink.attrs['href']
  43. author_and_email.append((author, email))return author_and_email
  44. defrun(self):# 初始化浏览器驱动print('Initiate driver ...')if self.extension_path isnotNone:
  45. chrome_options = webdriver.ChromeOptions()# 初始化Chrome选项
  46. chrome_options.add_extension(self.extension_path)# 安装学霸图书馆通道插件
  47. chrome_options.add_argument(r'user-data-dir=C:\Users\lenovo\AppData\Local\Google\Chrome\User Data')
  48. chrome_options.add_experimental_option('useAutomationExtension',False)
  49. driver = webdriver.Chrome(chrome_options=chrome_options)# 配置Chrome选项else:# 通常这种情况目前是不可行的, 不过以后可能又不需要插件了, 即可恢复为默认的火狐浏览器驱动更加稳定
  50. driver = webdriver.Firefox()
  51. driver.set_page_load_timeout(15)# 设置最长加载时间, 否则会卡死在论文详情页面
  52. driver.maximize_window()# 最大化窗口: 2021年新改变, 如果不最大化窗口, 搜索页面布置将会发生变化, 无法转为expert模式print(' - Complete !')# 登录学霸图书馆print('Login ...')
  53. driver.get('http://www.xuebalib.com')print(' - Waiting for textinput ...')
  54. WebDriverWait(driver,30).until(lambda driver: driver.find_element_by_xpath('//input[@name="username"]').is_displayed())print(' + OK !')print(' - Input username and password ...')
  55. driver.find_element_by_xpath('//input[@name="username"]').send_keys(self.username)
  56. driver.find_element_by_xpath('//input[@name="password"]').send_keys(self.password)
  57. driver.find_element_by_xpath('//input[@value="登陆"]').click()print(' + OK !')
  58. time.sleep(3)print(' - Complete !')# 取得通道访问权限: 2021年使用BUAA(北京航空航天大学)通道print('Get through Passageway ... (It is extremely slow and may be failed for several times)')
  59. driver.get('http://www.xuebalib.com/db.php/EI')print(' - Waiting for Passageway link ...')
  60. WebDriverWait(driver,30).until(lambda driver: driver.find_element_by_xpath('//a[contains(text(), "BUAA")]').is_displayed())print(' + OK !')print(' - Enter Passageway ...')
  61. driver.find_element_by_xpath('//a[contains(text(),"BUAA")]').click()print(' + OK !')print(' - Switch to new window ...')
  62. windows = driver.window_handles # 初始化窗口句柄print(f' + Totally {len(windows)} windows !')
  63. driver.switch_to.window(windows[1])# 转到新窗口print(' + OK !')print(' - Access to Engineering Village ... (It is the most difficult step and always failed)')
  64. WebDriverWait(driver,60).until(lambda driver: driver.find_element_by_xpath('//a[@href="https://www-engineeringvillage-com.e1.buaa.edu.cn"]').is_displayed())# 开始进行搜索
  65. count =0for index, keyword inenumerate(self.keywords):
  66. flag =1while flag <= self.max_trial_time:try:print(f' + No.{flag} Trial ...')
  67. driver.get('http://www.engineeringvillage.com')# 20211017更新: 之前为什么这个链接行不通, 因为没有把User Data加进来, 导致一直落在Welcome界面无法权限登录, 今早试出来要chrome_options.add_argument(r'user-data-dir=C:\Users\lenovo\AppData\Local\Google\Chrome\User Data'), 走这个通道更稳定# driver.get('https://www-engineeringvillage-com-443.e1.buaa.edu.cn/search/quick.url')# driver.find_element_by_xpath('//a[@href="https://www-engineeringvillage-com.e1.buaa.edu.cn"]').click()print(' + Waiting for search textinput ...')
  68. WebDriverWait(driver,60).until(lambda driver: driver.find_element_by_xpath('//input[@class="search-word"]').is_displayed())print(' + OK !')breakexcept Exception as e:
  69. flag +=1print(f' * Fail: {e}')continueprint(' - Complete !')# 再次确认已经进入Engineering Villageprint('Waiting for search textinput again ...')
  70. WebDriverWait(driver,60).until(lambda driver: driver.find_element_by_xpath('//input[@class="search-word"]').is_displayed())print(' - Complete !')if self.mode =='expert':# export模式下转为专家搜索print('Switch to expert mode ...')
  71. driver.find_element_by_xpath('//span[@class="button-link-text" and contains(text(),"Search")]').click()
  72. time.sleep(1)
  73. driver.find_element_by_xpath('//span[@class="button-link-text" and contains(text(),"Expert")]').click()print(' - Complete !')print(f'Search keyword: {keyword}')withopen(f'keyword_{index}.txt','w', encoding='utf8')as f:pass# 重置搜索框print(' - Reset textinput ...')
  74. xpath ='//a[@id="reset-form-link-quick"]'if self.mode =='quick'else'//a[@id="reset-form-link-expert"]'
  75. driver.find_element_by_xpath(xpath).click()
  76. time.sleep(2)print(' + OK !')# 输入关键词print(' - Input keyword ...')
  77. xpath ='//input[@class="search-word"]'if self.mode =='quick'else'//textarea[@class="search-word text-area-lg"]'
  78. driver.find_element_by_xpath(xpath).send_keys(keyword)
  79. time.sleep(2)print(' + OK !')# 点击搜索print(' - Click search engine ...')
  80. xpath ='//a[@id="searchBtn"]'if self.mode =='quick'else'//a[@id="expertSearchBtn"]'
  81. driver.find_element_by_xpath(xpath).click()print(' + OK !')# 等待搜索结果print(' - Waiting for search results ...')
  82. WebDriverWait(driver,60).until(lambda driver: driver.find_element_by_xpath('//a[@class="detaillink"]').is_displayed())
  83. html = driver.page_source
  84. soup = BeautifulSoup(html,'lxml')
  85. h2 = soup.find('h2',id='results-count')for child in h2.children:
  86. results_count =int(str(child).strip())breakprint(f' + Totally {results_count} results !')
  87. current_url = driver.current_url # 记录当前URL, 便于再次回到该页面
  88. time.sleep(3)print(' + OK !')# *** 试图调整下拉框每页显示数量100, 这样可以少翻几页, 但是不知为何无法使用Select方法# driver.find_element_by_xpath('//span[@class='select2-selection__arrow']').click()# time.sleep(2)# select_el = Select(driver.find_element_by_xpath('//select[@id='results-per-page-select']'))# select_el.select_by_visible_text('100')
  89. current_index =1# 记录搜索结果的序号
  90. page_number =0# 记录分页值whileTrue:
  91. page_number +=1# 遍历每一页搜索结果print(f' - Page {page_number} ...')
  92. html = driver.page_source
  93. soup = BeautifulSoup(html,'lxml')
  94. detailurls = Xuebalib.get_detailurl(soup)for detailurl in detailurls:
  95. count +=1print(f' + Processing No.{count} paper ...')try:# 可能会失败: 原因是来自配置driver.set_page_load_timeout(10), 但是不配置页面最长加载时间, 其他地方也可能会报错
  96. driver.get(self.host + detailurl)
  97. WebDriverWait(driver,30).until(lambda driver: driver.find_element_by_xpath('//ul[@class="abs_authors"]').is_displayed())except:# 虽然加载不完全, 不过无所谓反正需要的信息大概率都已经加载出来了print(' + Load incompletely ! (Do not care about this)')# 解析作者及邮箱
  98. html = driver.page_source
  99. soup = BeautifulSoup(html,'lxml')
  100. author_email_pairs = Xuebalib.get_author_and_email(soup, ignore=False)# 将作者及邮箱写入文件print(' + Write to file ...')for author,email in author_email_pairs:withopen(f'keyword_{index}.txt','a', encoding='utf8')as f:
  101. f.write(f'{author}\t{email}\n')print(' + OK !')# 回到搜索结果页面并点击下一页: 这种方法不太稳定# driver.get(current_url)# try: # 如果已经到最后一页, 就会发现没有不存在下一页按钮了# WebDriverWait(driver, 30).until(lambda driver: driver.find_element_by_xpath('//a[@id='next-page-top']').is_displayed())# except: # 此时退出循环# break# driver.find_element_by_xpath('//a[@id='next-page-top']').click()# WebDriverWait(driver, 30).until(lambda driver: driver.find_element_by_xpath('//a[@class='detaillink']').is_displayed())# current_url = driver.current_url # 所以想了个好办法, 可以直接通过改变查询字符串转到下一页
  102. current_index +=25# 默认每页25个, 因为上面下拉框改成100个的逻辑总是失败, 所以以后分页数量变了得自己记得改if current_index > results_count:# 一旦超过之前记录的搜索结果数量就表明该关键词已经爬取完毕break
  103. index1 = current_url.find('COUNT=')
  104. index2 = current_url.find('&', index1)
  105. next_url = current_url[: index1 +6]+str(current_index)+ current_url[index2:]whileTrue:try:print(f' - Switch to next page (next page is {page_number +1})...')
  106. driver.get(next_url)print(' + Waiting for search results ...')
  107. WebDriverWait(driver,30).until(lambda driver: driver.find_element_by_xpath('//a[@class="detaillink"]').is_displayed())print(' + OK !')breakexcept Exception as e:print(f' + Fail: {e} ...')continueif __name__ =='__main__':
  108. keywords = load_keywords('kw.txt')print(keywords)
  109. username =''# 学霸图书馆用户名
  110. password =''# 学霸图书馆密码# host = 'https://www-engineeringvillage-com-443.e1.buaa.edu.cn' # 学霸图书馆用于访问Engineering Village的大学主机URL: 2020年使用的是沈阳工业大学的主机(http://202.199.103.219), 2021年最新使用的是北京航空航天大学的主机
  111. host ='http://www.engineeringvillage.com'# 20211017更新: 可以跳过通道直接访问
  112. extension_path ='D:/xuebalib.crx'# 学霸图书馆用于通道访问的插件: 由于浏览器驱动默认不会带插件, 即便浏览器上已经安装了自动启动的插件, 在驱动时也不会启用插件, 因此需要每次加载插件
  113. mode ='expert'# 建议使用export模式而非默认的quick模式, 因为前者相对不容易出错
  114. xuebalib = Xuebalib(keywords=keywords,
  115. username=username,
  116. password=password,
  117. host=host,
  118. extension_path=extension_path,
  119. mode=mode)
  120. xuebalib.run()

后记

诸事安好,望君莫虑。

很久,很久,不见了呢。

标签: selenium 爬虫 python

本文转载自: https://blog.csdn.net/CY19980216/article/details/120808137
版权归原作者 囚生CY 所有, 如有侵权,请联系我们删除。

“【日常】爬虫进阶技巧:selenium加载扩展插件(extension)与配置用户数据(user-data)”的评论:

还没有评论