Python challenge game - mission2 第二关

打开http://www.pythonchallenge.com/pc/def/ocr.html:

在这里插入图片描述
recognize the characters. maybe they are in the book,
but MAYBE they are in the page source.

赤果果的告诉我们在源代码中:

于是可以用request获取源代码:

import requests


def get_html(url):
    r = requests.get(url, timeout = 30)
    r.raise_for_status()
    r.encoding = r.apparent_encoding
    return r.text

if __name__ == '__main__':
    txt = get_html('http://www.pythonchallenge.com/pc/def/ocr.html')
    txt = txt.lower()
    for i in '~!@#$%^&*()_+{}|:<>?`[]\'";,./\n':
        txt = txt.replace(i, '')
    print(txt)```


htmlhead titleocrtitle link rel=stylesheet type=textcss href=stylecssheadbodycenterimg src=ocrjpgbrfont color=c03000recognize the characters maybe they are in the book brbut maybe they are in the page sourcecenterbrbrbrfont size=-1 color=goldgeneral tipsliuse the hints they are helpful most of the timesliliinvestigate the data given to youliliavoid looking for spoilerslibrforums a href=httpwwwpythonchallengecomforumspython challenge forumsa read before you postbrirc ircfreenodenet pythonchallengebrbrto see the solutions to the previous level replace pc with pcc ie go to httpwwwpythonchallengecompccdefocrhtmlbodyhtml–find rare characters in the mess below----equality–

得到equality, 下一关入口,其实我这里代码不对,但是目前还不清楚如何专门获取注释,没有屏蔽equality之前的内容,希望大神指点(2019/6/31)

-----------------2017/07/21更新---------------------

一步到位提取

import requests
from lxml import etree


def get_html(url):
    r = requests.get(url, timeout=30)
    r.raise_for_status()
    r.encoding = r.apparent_encoding
    return r.text


if __name__ == '__main__':
    txt1 = get_html('http://www.pythonchallenge.com/pc/def/ocr.html')
    response = etree.HTML(txt1)
    txt = response.xpath('//comment()')[1]
    for i in '~-!@#$%^&*()_+{}|:<>?`[]\'";,./\n':
        txt = str(txt).replace(i, '')
    print(txt)


直接显示结果:equality

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值