这是报的错误
这是结构,就二个py文件是自己敲的,其他都是生成的
代码
getCommeInfo.py
#!/usr/bin/evn python3
# -*- coding:utf-8 -*-
import urllib.request
import urllib.parse
from bs4 import BeautifulSoup
from mylog import MyLog as mylog
import codecs
class Item(object):
title=None #帖子标题
firstAuthor=None #帖子创作者
firstTime=None #帖子创建时间
reNum=None #总回复数
content=None #最后回复内容
lastAuthor=None #最后回复者
lastTime=None #最后回复时间
class GetTiebaInfo(object):
def __init__(self,url):
self.url=url
self.log=mylog()
self.pageSum=5
self.urls=self.getUrls(self.pageSum)
self.items=self.spider(self.urls)
self.pipelines(self.items)
def getUrls(self,pageSum):
urls=[]
pns=[str(i*50) for i in range(pageSum)]
ul=self.url.split('=')
for pn in pns:
ul[-1]=pn
url='='.join(ul)
urls.append(url)
self.log.info('获取URLS成功')
return urls
def spider(self, urls):
items=[]
for url in urls:
htmlContent=self.getResponseContent(url)
soup=BeautifulSoup(htmlContent,'lxml')
tagsli=soup.find_all('li',attrs={'class':'j_thread_listclearfix'})
for tag in tagsli:
item=Item()
item.title=tag.find('a',attrs={'class':'j_th_tit'}).get_text().strip()
item.firstAuthor=tag.find('span',attrs={'class':'frs-author-name-wrap'}).a.get_text().strip()
item.firstTime=tag.find('span',attrs={'title':'创建的时间'}).get_text().strip()
item.reNum=tag.find('span',attrs={'title':'回复'}).get_text().strip()
item.content=tag.find('div',attrs={'class':'threadlist_abs threadlist_abs_onlyline'}).get_text().strip()
item.lastAuthor=tag.find('span',attrs={'class':'tb_icon_author_rely j_replyer'}).a.get_text().strip()
item.lastTime=tag.find('span',attrs={'title':'最后回复时间'}).get_text().strip()
items.append(item)
self.log.info('获取标题为<<%s>>的项成功...' %item.title)
return items
def pipelines(self, items):
fileName='百度贴吧_权利的游戏.txt'#.encode('utf-8')
with codecs.open(fileName, 'w', 'utf-8') as fp:
for item in items:
try:
fp.write('title:%s \t author:%s \t firstTime:%s \r\n content:%s \r\n return:%s \r\n lastAuthor:%s \t lastTime:%s \r\n\r\n\r\n\r\n'%(item.title, item.firstAuthor, item.firstTime, item.content, item.reNum, item.lastAuthor, item.lastTime))
except Exception as e:
self.log.error('写入文件失败')
else:
self.log.info('标题为<<%s>>的项输入到"%s"成功'%(item.title, fileName))
def getResponseContent(self, url):
urlList=url.split('=')
urlList[1]=urllib.parse.quote(urlList[1])
url='='.join(urlList)
try:
response=urllib.request.urlopen(url)
except:
self.log.error('Python 返回URL:%s 数据失败' %url)
else:
self.log.info('Python 返回URUL:%s 数据成功' %url)
return response.read()
if __name__=='__main__':
url='http://tieba.baidu.com/f?kw=权利的游戏&ie=utf-8&pn=50'
GTI=GetTiebaInfo(url)
mylog.py
#!/usr/bin/evn python3
# -*- coding: UTF-8 -*
import logging
import getpass
import sys
###定义MyLog类
class MyLog(object):
###类MyLog的构造函数
def __init__(self):
self.user=getpass.getuser()
self.logger=logging.getLogger(self.user)
self.logger.setLevel(logging.DEBUG)
###日志文件名
self.logFile=sys.argv[0][0:-3]+'.log'
self.formatter=logging.Formatter('%(asctime)-12s %(levelname)-8s %(name)-10s %(message)-12s\r\n')
###日志显示到屏幕上并输出到日志文件内
self.logHand=logging.FileHandler(self.logFile, encoding='utf8')
self.logHand.setFormatter(self.formatter)
self.logHand.setLevel(logging.DEBUG)
self.logHandSt=logging.StreamHandler()
self.logHandSt.setFormatter(self.formatter)
self.logHandSt.setLevel(logging.DEBUG)
self.logger.addHandler(self.logHand)
self.logger.addHandler(self.logHandSt)
###日志的五个级别对应以下的五个函数
def debug(self,msg):
self.logger.debug(msg)
def info(self,msg):
self.logger.info(msg)
def warn(self,msg):
self.logger.warn(msg)
def error(self,msg):
self.logger.error(msg)
def critical(self,msg):
self.logger.critical(msg)
if __name__=='__main__':
mylog=MyLog()
mylog.debug(u"I'm debug 测试中文")
mylog.info("I'm info")
mylog.warn("I'm warn")
mylog.error(u"I'm error 测试中文")
mylog.critical("I'm critical")
完全一模一样,照着书敲得,对着检车一天了,一个字也没错。求求救救孩子,错误的中文翻译为:NoneType' 'NoneType' 对象不可迭代,百度看了好多类似的都和我的不一样。
救命救命,老公!