关于这个该死的报错:TypeError - 'undefined' is not a function (evaluating '_getTagName(currWindow).toLowerCase()')
在利用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行代码写死,然而要是不加判断也会出现这样的报错,比较郁闷,后来查了半天资料,在Stackoverflow的评论中找到思路,很有可能是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,请忽略渣渣排版
关于这个该死的报错:TypeError - 'undefined' is not a function (evaluating '_getTagName(currWindow).toLowerCase()')的更多相关文章
- jquery TypeError: 'undefined' is not a function (evaluating 'elem.nodeName.toLowerCase()') [jquery.js:1904]错误原因
		
今天,某个环境报了个js错误,TypeError: 'undefined' is not a function (evaluating 'elem.nodeName.toLowerCase()') [ ...
 - react-native 编译报错: undefined is not an object (evaluating '_react2.PropTypes.func')
		
情况通报: 因为是我的二维码模块报错,提示报错代码如下 重要信息是下面的红色字体部分(Android 模拟器红屏) undefined is not an object (evaluating '_r ...
 - 【已解决】React项目中按需引入ant-design报错TypeError: injectBabelPlugin is not a function
		
react项目中ant-design按需加载,使用react-app-rewired的时候报错 运行npm start或者yarn start报如下错误: TypeError: injectBabel ...
 - 报错TypeError: $(...).live is not a function解决方法
		
报错的原因是这个方法在jquery1.7以后就被废除了, 1.7以后的版本改用.on()方法 之前的用法: .live(events, function) 新方法: .on(eventType, se ...
 - flvjs的unload(),detachMediaElement(),destroy()报错,undefined,not a function解决方案
		
首先,真的被网上一堆各种转载复制粘贴坑了不少,最后直接到GitHub上flvjs的看作者的demo和docs才解决,具体如下. 1.引入flvjs文件,新建实例 var flvUrl = '直播流地址 ...
 - layui的layer独立版报错“TypeError: i is not a function”的解决
		
折腾良久发现是引入jQuery顺序的问题. jQuery必须在layer引入之前引入.
 - [转载]UEditor报错TypeError: me.body is undefined
		
本文转载来自:UEditor报错TypeError: me.body is undefined 今天在使用UEditor的setContent的时候报错,报错代码如下 TypeError: me.bo ...
 - VUE.JS 使用axios数据请求时数据绑定时 报错 TypeError: Cannot set property 'xxxx' of undefined 的解决办法
		
正常情况下在data里面都有做了定义 在函数里面进行赋值 这时候你运行时会发现,数据可以请求到,但是会报错 TypeError: Cannot set property 'listgroup' of ...
 - Node中使用MySQL报错:TypeError: Cannot read property 'query' of undefined
		
Node中使用MySQL报错: TypeError: Cannot read property 'query' of undefined at /Users/sipeng/Desktop/彭思/201 ...
 
随机推荐
- java 世界中Annotation
			
java 世界中Annotation 在github上开始汇总一些自己学习,收集,总结,经验的一些信息,有利于跟踪,修改,提升.如果你感兴趣 可以关注一下,也可以提供自己的内容进来. https:// ...
 - Hadoop学习笔记五
			
一.uber(u:ber)模式 MapReduce以Uber模式运行时,所有的map,reduce任务都在一个jvm中运行,对于小的mapreduce任务,uber模式的运行将更为高效. uber模式 ...
 - python基础   列表  的使用
			
列表 首先定义一个列表 声明列表 列表名字=[值1,值2] list=[1,2,3,4,5] 这是一个列表,列表中有五个元素(1,2,3,4,5) 显示list列表 print list 输出 ...
 - BZOJ 1778: [Usaco2010 Hol]Dotp 驱逐猪猡 [高斯消元 概率DP]
			
1778: [Usaco2010 Hol]Dotp 驱逐猪猡 题意:一个炸弹从1出发p/q的概率爆炸,否则等概率走向相邻的点.求在每个点爆炸的概率 高斯消元求不爆炸到达每个点的概率,然后在一个点爆炸就 ...
 - S5PV210时钟,看门狗定时器
			
晶振:时钟源(操作主要有两个,倍频,分频) A8的时钟源: 时钟域,每个时钟域(不同的最高频率和最低频率)管理着不同的电路模块: 不同的时钟域对应不同电路模块表 时钟电路:懂得看时钟电路(时钟源选择开 ...
 - Hive metastore整体代码分析及详解
			
从上一篇对Hive metastore表结构的简要分析中,我再根据数据设计的实体对象,再进行整个代码结构的总结.那么我们先打开metadata的目录,其目录结构: 可以看到,整个hivemeta的目录 ...
 - 初识vps,域名与购买,初步配置
			
终于还是到了这一天,不管我们是不是程序员,当我们想拥有自己的一个的博客,当我们想有自己的一个空间,当我们想在网上有一个自己可以随心所欲编写任何不被限制的仅仅是酷炫的效果,当我们想收录自己的技术,经历, ...
 - JS工厂模式开发实践
			
JS工厂模式开发实践 基于JS工厂模式的H5应用,实现了轮播图功能与滑屏功能,并且实现了文字大小的自适应功能,基于SASS样式开发. 核心的JS代码如下: index.js define(functi ...
 - 抛砖引玉之~sftp
			
跨平台系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#linux XFtp用惯了,没有它就不行了?Linux就不能文件传输了? 答案肯定是~No ...
 - c++ 如何获取多线程的返回值?
			
// Console.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <stdlib.h> #include ...