最早在做Pythonchallenge时我参考了下文的代码
https://blog.youkuaiyun.com/zrzlj/article/details/48805433
但这篇文章并不是基于Python2,有一些语法不适用
其中会发生报错
startswith first arg must be bytes or a tuple of bytes, not str
应该是说startswith只能识别bytes,不能识别str
这似乎是因为urlopen().read(),返回的是bytes类型
根据这篇文章
https://www.jianshu.com/p/efcff000b81a
Python3应该是区分了字节与字符串
所以我对代码进行了修改,下面是可以正常运行的代码
import urllib
def main():
resp = urllib.request.urlopen("http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=63579").read()
t = 0
while True:
print (t)
t = t+1
print (resp)
if t >= 400:
break
if resp.startswith(b"<font color=red>Your hands are getting tired </font>and the next nothing is "):
resp = resp.replace(b"<font color=red>Your hands are getting tired </font>and the next nothing is ",b"")
print(resp.decode())
resp = urllib.request.urlopen("http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing="+resp.decode()).read()
print(resp)
elif resp.startswith(b"and the next nothing is "):
resp = resp.replace(b"and the next nothing is ", b"")
resp = urllib.request.urlopen("http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing="+resp.decode()).read()
else:
print (resp)
break
if __name__ == '__main__':
main()
这里对网页内容读取、判断、修改的resp变量一直为bytes型,之后因为要加入url需要转换为str型,所以参考这篇文章
https://www.cnblogs.com/niuu/p/10106897.html
我使用resp.decode进行转换