Python 2.7 属性错误:';非类型';对象没有属性';查找';使用BeautifulSoup的python 2.7
我刚刚开始python爬行,并且已经尝试爬行web文本一个月了。 我在Python2.7.13中尝试了这段代码,它以前工作得很好Python 2.7 属性错误:';非类型';对象没有属性';查找';使用BeautifulSoup的python 2.7,python-2.7,Python 2.7,我刚刚开始python爬行,并且已经尝试爬行web文本一个月了。 我在Python2.7.13中尝试了这段代码,它以前工作得很好 class IEEECrawler: def __init__(self): self.baseUrl = "http://ieeexplore.ieee.org" self.targetUrl = "http://ieeexplore.ieee.org/xpl/mostRecentIssue.jsp?reload=true&a
class IEEECrawler:
def __init__(self):
self.baseUrl = "http://ieeexplore.ieee.org"
self.targetUrl = "http://ieeexplore.ieee.org/xpl/mostRecentIssue.jsp?reload=true&filter%3DAND%28p_IS_Number%3A4359286%29&rowsPerPage=100&pageNumber=1&resultAction=REFINE&resultAction=ROWS_PER_PAGE&isnumber=4359286#1.html"
self.soup = BeautifulSoup(urllib.urlopen(self.targetUrl).read(), "lxml")
self.doc_list = self.soup.find_all('div', {'class': "txt"})
self.subUrl = []
def crawlOriginalPage(self):
file = open("./result.txt", "w")
for doc in self.doc_list:
head = doc.find("h3")
author_list = ''
for author in doc.find_all("div", {'class':"authors"}):
for tt in author.find_all('span', {'id':"preferredName"}):
author_list += tt['data-author-name'] + ";"
author_list = author_list[:-1]
file.write(head.find("span").text + ';')
file.write(author_list.strip() + ';')
file.write(self.baseUrl+head.find('a')['href']+ ';')
file.write(doc.find("div", {'class': "hide abstract RevealContent"}).find("p").text.replace('View full abstract'+'»'.decode('utf-8'),'').strip()+ '\n')
file.close()
print 'finish'
然而,今天我再次运行了这段代码,我没有处理这个错误masseges。我不知道应该修改什么代码
Traceback (most recent call last):
File "/Users/user/Downloads/ieee_fin/ieee.py", line 35, in <module>
crawler.crawlOriginalPage()
File "/Users/user/Downloads/ieee_fin/ieee.py", line 29, in crawlOriginalPage
file.write(doc.find("div", {'class': "hide abstract RevealContent"}).find("p").text.replace('View full abstract'+'»'.decode('utf-8'),'').strip()+ '\n')
AttributeError: 'NoneType' object has no attribute 'find'
回溯(最近一次呼叫最后一次):
文件“/Users/user/Downloads/ieee_fin/ieee.py”,第35行,在
crawler.crawoloriginalpage()
文件“/Users/user/Downloads/ieee_fin/ieee.py”,第29行,在crawlOriginalPage中
file.write(doc.find(“div”,{'class':“hide abstract RevealContent”}).find(“p”).text.replace('View full abstract'+'»).decode('utf-8'),'.strip()+'\n')
AttributeError:“非类型”对象没有属性“查找”
错误会显示以下行:
file.write(doc.find("div", {'class': "hide abstract RevealContent"}).find("p").text.replace('View full abstract'+'»'.decode('utf-8'),'').strip()+ '\n')
只需查找方法find
(有2个)并查看前面的内容
也许这不好:
doc.find(...)
doc.find("div", {'class': "hide abstract RevealContent"}).find("p")
这意味着doc
是非类型,这意味着doc
是None
或者这可能不好:
doc.find(...)
doc.find("div", {'class': "hide abstract RevealContent"}).find("p")
这意味着doc.find(…class…
正在返回None
。可能是因为它找不到
总之,您可能需要在该代码周围放置一个
try…except
包装,或者将其分解一点,然后开始检查无
文档
是否为空。调试以找出原因。谢谢你,奥斯汀。你的评论真的很有帮助。我稍后会尝试发布有关它的信息。