热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用python3.x对pythonchallenge-----4的解答过程

pythonchallenge-4地址:http:www.pythonchallenge.compcdeflinkedlist.php图片如下:题目解析:通过页面源代码解析,要打开链
pythonchallenge-4地址 : http://www.pythonchallenge.com/pc/def/linkedlist.php
图片如下:

题目解析:通过页面源代码解析,要打开链接http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345,然后获取nothing值,一直循环直到得出答案
解题过程:

from urllib import request,parse
import re

url = r'http://www.pythonchallenge.com/pc/def/linkedlist.php?'

headers = {
    'User-Agent': r'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
                    r'Chrome/45.0.2454.85 Safari/537.36 115Browser/6.0.3',
    'Connection': 'keep-alive'
}
data = {}
data['nothing'] = "12345"

def getnothing(url,data,headers):
    data = parse.urlencode(data)
    url = url + data
    req = request.Request(url,headers=headers)
    page = request.urlopen(req).read().decode('utf-8')
    return page

for i in range(251):
    htmlstr = getnothing(url=url,data=data,headers=headers)
    pattern = re.compile('\d+')
    nothingnum = re.findall(pattern,htmlstr)
    if nothingnum:
        data['nothing'] = nothingnum[0]
        if len(nothingnum)>1:
            data['nothing'] = nothingnum[1]
    else:
        data['nothing'] = str(int(data['nothing'])/2)
    print(str(i) + " ---- " + htmlstr)
    print(data['nothing'])

 

 答案:

250 ---- peak.html

 心得:在第85次和第140次的时候分别有个小坑

85 ---- Yes. Divide by two and keep going
...
...
...
140 ---- There maybe misleading numbers in the 
text. One example is 82683. Look only for the next nothing and the next nothing is 63579

 

 

 
 

推荐阅读
author-avatar
咖啡十伴侣
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有