关于这个该死的报错:TypeError - 'undefined' is not a function (evaluating '_getTagName(currWindow).toLowerCase()')

时间:2021-12-14 23:03:38

在利用Selenium爬取页面信息的时候突然报错,第一条信息爬取的时候还好好的,第二条就不行了。

请参考网上的爬取代码:

 # coding=utf-8
"""
Created on 2015-12-10 @author: Eastmount
利用Selenium爬取百度百科5A级景区的内容介绍的代码
""" import time
import re
import os
import sys
import codecs
import shutil
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.action_chains import ActionChains # Open PhantomJS
# driver = webdriver.PhantomJS(executable_path="D:\phantomjs-2.1.1-windows\sbin\phantomjs.exe")
driver = webdriver.PhantomJS(executable_path="D:\phantomjs-1.9.8-windows\phantomjs.exe")
# driver = webdriver.Firefox()
wait = ui.WebDriverWait(driver, 10) # 显示等待时间(实例,最大等待时间) # Get the Content of 5A tourist spots
def getInfobox(entityName, fileName):
try:
# create paths and txt files
print(u'文件名称: ', fileName)
info = codecs.open(fileName, 'w', 'utf-8') # locate input notice: 1.visit url by unicode 2.write files
# Error: Message: Element not found in the cache
# Perhaps the page has changed since it was looked up
# 解决方法: 使用Selenium和Phantomjs print(u'实体名称: ', entityName.rstrip('\n'))
driver.get("http://baike.baidu.com/")
elem_inp = driver.find_element_by_xpath("//form[@id='searchForm']/input")
# elem_inp = driver.find_elements_by_xpath("//div[@class='lemma-summary']/div")
elem_inp.send_keys(entityName)
elem_inp.send_keys(Keys.RETURN)
info.write(entityName.rstrip('\n') + '\r\n') # codecs不支持'\n'换行 # load content 摘要
elem_value = driver.find_elements_by_xpath("//div[@class='lemma-summary']/div")
for value in elem_value:
print(value.text)
info.writelines(value.text + '\r\n') # 爬取文本信息
# 爬取所有段落<div class='para'>的内容 class='para-title'为标题 [省略] time.sleep(2)
# except Exception as e: # 'utf8' codec can't decode byte
# print("Error: ", e)
finally:
print('\n')
info.close() # Main function
def main():
# By function get information
path = "BaiduSpider\\"
if os.path.isdir(path):
shutil.rmtree(path, True)
os.makedirs(path)
source = open("Tourist_spots_5A.txt", 'r')
num = 1
for entityName in source:
# entityName = unicode(entityName, "utf-8")
if u'故宫' in entityName: # else add a '?'
entityName = '北京故宫'
# else: Name = entityName.rstrip('\n')
name = "%04d" % num
fileName = path + str(name) + ".txt"
getInfobox(entityName, fileName)
num = num + 1
print('End Read Files!')
source.close()
driver.close() if __name__ == '__main__':
main()

执行报错信息为:

Traceback (most recent call last):
File "D:/pycharm/untitled_DB/wordcloud/selenium爬取百度百科/Selenium_baidu.py", line 85, in <module>
main()
File "D:/pycharm/untitled_DB/wordcloud/selenium爬取百度百科/Selenium_baidu.py", line 77, in main
getInfobox(entityName, fileName)
File "D:/pycharm/untitled_DB/wordcloud/selenium爬取百度百科/Selenium_baidu.py", line 41, in getInfobox
elem_inp.send_keys(Keys.RETURN)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webelement.py", line 479, in send_keys 'value': keys_to_typing(value)})
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
return self._parent.execute(command, params)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 208, in check_response
raise exception_class(value)
selenium.common.exceptions.WebDriverException: Message: TypeError - 'undefined' is not a function (evaluating '_getTagName(currWindow).toLowerCase()')

找了1天都没找到原因,真的 死烦 ,找到原因是71行代码写死,然而要是不加判断也会出现这样的报错,比较郁闷,后来查了半天资料,在*的评论中找到思路,很有可能是read文件的时候,读取到的内容格式有问题,于是查看了一下格式发现,果不其然,多了一个"/n",修改代码:

if u'故宫' in entityName:  # else add a '?'
  entityName = '北京故宫'
else:
  entityName = entityName.rstrip('\n')
name = "%04d" % num
fileName = path + str(name) + ".txt"
getInfobox(entityName, fileName)
num = num + 1

在执行,ok,请忽略渣渣排版