关于python Challenge level 4

NoFun的Exception 2016-11-15 11:08:24
大家好,看到PythonChallenge level 4之后,(问题是,将上一网页中的信息中的一部分作为新的网页的连接的一部分,一直进行下去寻找线索)
想通过递归来找找思路,但是遇到了一些问题。
代码还不完善,只是测试到第二个code后面就出现一直重复不生成新的code的情况,请问这是怎么回事呢?
下面是我的代码:

import urllib.request
import re

url = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345' #作为最初的url

def visit(url):
Request = urllib.request.urlopen(url)
html = Request.read().decode('utf-8')
Request.close()
Purl = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=' #把url看作为前面一部分不变的字符串加上后面的数字组成的字符串
code = str(re.findall(r'(\d+$){1}',html)) #匹配最后的数字作为code和Purl组成新的url
print('%s'%code) #测试获得的code
url = Purl+code
visit(url) #将新的url带入函数继续

return None

visit(url)
...全文
143 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
NoFun的Exception 2016-11-15
  • 打赏
  • 举报
回复
引用 2 楼 sinat_30665603 的回复:
#coding: utf-8 import urllib2 import re url = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345' #作为最初的url def visit(url): Request = urllib2.urlopen(url) html = Request.read().decode('utf-8') Request.close() Purl = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=' #把url看作为前面一部分不变的字符串加上后面的数字组成的字符串 code = str(re.findall(r'(\d+$){1}',html)[0]) #匹配最后的数字作为code和Purl组成新的url print('%s'%code) #测试获得的code url = Purl+code visit(url) #将新的url带入函数继续 return None visit(url)
感谢感谢,Py3+好像没有区分Urllib2了,同意都用的Urllib3,[0]感谢指出重点
NoFun的Exception 2016-11-15
  • 打赏
  • 举报
回复
引用 1 楼 IamNieo 的回复:

#code = str(re.findall(r'(\d+$){1}',html))
code = str(re.findall(r'(\d+$){1}',html)[0])
非常感谢,,看来我对str,list的操作还没弄清楚啊。
斯温jack 2016-11-15
  • 打赏
  • 举报
回复
#coding: utf-8 import urllib2 import re url = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345' #作为最初的url def visit(url): Request = urllib2.urlopen(url) html = Request.read().decode('utf-8') Request.close() Purl = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=' #把url看作为前面一部分不变的字符串加上后面的数字组成的字符串 code = str(re.findall(r'(\d+$){1}',html)[0]) #匹配最后的数字作为code和Purl组成新的url print('%s'%code) #测试获得的code url = Purl+code visit(url) #将新的url带入函数继续 return None visit(url)
nieoding 2016-11-15
  • 打赏
  • 举报
回复

#code = str(re.findall(r'(\d+$){1}',html))
code = str(re.findall(r'(\d+$){1}',html)[0])
Gain a fundamental understanding of Python’s syntax and features with the second edition of Beginning Python, an up–to–date introduction and practical reference. Covering a wide array of Python–related programming topics, including addressing language internals, database integration, network programming, and web services, you’ll be guided by sound development principles. Ten accompanying projects will ensure you can get your hands dirty in no time. Updated to reflect the latest in Python programming paradigms and several of the most crucial features found in the forthcoming Python 3.0 (otherwise known as Python 3000), advanced topics, such as extending Python and packaging/distributing Python applications, are also covered. What you’ll learn * Become a proficient Python programmer by following along with a friendly, practical guide to the language’s key features. * Write code faster by learning how to take advantage of advanced features such as magic methods, exceptions, and abstraction. * Gain insight into modern Python programming paradigms including testing, documentation, packaging, and distribution. * Learn by following along with ten interesting projects, including a P2P file–sharing application, chat client, video game, remote text editor, and more. Complete, downloadable code is provided for each project! Who is this book for? Programmers, novice and otherwise, seeking a comprehensive introduction to the Python programming language. About the Apress Beginning Series The Beginning series from Apress is the right choice to get the information you need to land that crucial entry–level job. These books will teach you a standard and important technology from the ground up because they are explicitly designed to take you from “novice to professional.” You’ll start your journey by seeing what you need to know—but without needless theory and filler. You’ll build your skill set by learning how to put together real–world projects step by step. So whether your goal is your next career challenge or a new learning opportunity, the Beginning series from Apress will take you there—it is your trusted guide through unfamiliar territory!

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧