贴吧例子代码来源于csdn网站,感谢作者技术分享,受益匪浅。
#encoding=utf8
import string, urllib2
def baidu_tieba(url, begin_page, end_page):
for i in range(begin_page,end_page+1):
sName = string.zfill(i,5)+'.html'
print '正在下载'+str(i)+ '个网页'
f = open('data/'+sName,'w+')
m = urllib2.urlopen(url+str(i)).read()
f.write(m)
f.close()
# bdulr = 'http://tieba.baidu.com/p/2296017831?pn='
bdulr = 'http://tieba.baidu.com/p/2494746884?pn='
begin_page = 1
end_page = 10
# bdulr = str(input(u'please input url:\r\n'))
# begin_page = int(input(u'input start pagenum'))
# end_page = int(input(u'end page num'))
baidu_tieba(bdulr,begin_page,end_page)
碰到的一个问题就是:
sublime2, 对raw_input()的总是提示:error, 貌似没有好的解决办法
然后使用eclipse, pydev, 但是很多时候api不支持,总是提示出错,比如
from twisted.internet.protocol import Protocol,Factory
from twisted.internet import reactor
class Echo(Protocol):
def dataReceived(self, data):
self.transport.write('hello: {}'.format(data))
class EchoFactory(protocol.Factory):
def buildProtocal(self,addr):
return Echo()
def connectionLost(self, reason):
print 'connect lost',reason
def main():
f = Factory()
f.protocol = Echo
reactor.listenTCP(8000,f) #语法提示错误,不影响执行
reactor.run()
if __name__ == '__main__':
main()
另外一种情况就是编译出错,比如
import binascii
import socket
import struct
import sys
#create a TCP/IP socket
# sock = socket.socket(socket.AF_INET6,socket.SOCK_STREAM)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_addr = ('',10000)
sock.bind(server_addr)
sock.listen(10)
unpacker = struct.Struct('I 2s f')
while True:
print >>sys.stderr , '\n waiting for a client'
conn, client_addr = sock.accept()
try:
data = conn.recv(unpacker.size())
unpacked_data = unpacker.unpack(data)
finally:
conn.close()
报错信息如下:
Traceback (most recent call last):
File "/Users/chenxu/work/python/pythontutorial/src/socket/server.py", line 14, in <module>
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
AttributeError: 'module' object has no attribute 'socket'
但是上面的代码使用sublime可以运行。
综合的办法就是
1. 使用eclipse, pydev来编写代码,这个ide有强大的代码补全功能,对于不熟悉python语法的人来说是福音
2. 使用sublime来运行代码,Command+b 运行快捷键
人生不如意,十有八九。
接受不能改变的事情吧。