有用的涨知识网有用的涨知识网

有用的涨知识网
一起学习分享有用的涨知识网

“抄”标识符,非但不必上Google分页了

程式设计派,优先选择设为

高质量该文,第三天数送抵!

先问我们两个难题,假如你撸标识符的操作过程中碰到了两个难题不晓得是不是化解,你通常会是不是办?

那总之是搜了,去这儿搜呢?

比如说这儿我就想晓得 Python 里头是不是采用 requests 是不是上载文档,我可能将就间接 Google 呵呵,结论有许多,比如说非官方文档格式,Stack Overflow 之类,接着两个个去查。

比如说我可能将就会输出关键字python requests upload file,搜到的结论类似于如下表所示:

这儿给我的第两个结论是 Stack Overflow,结论镜像为https://stackoverflow.com/questions/22567306/python-requests-file-upload,我把它登出来,接着找出最低票的标准答案。

这儿最低票的标准答案如图小事,接着把它的标识符 copy 留下来试一试看,接着就跑通了。

但是或许却是有一点麻烦事的?开应用程序,开 Google 或 Stack Overflow,找最低票标准答案试一试。

如是说

那时给我们如是说两个宝物,叫作 howdoi,有了它,或许他们就能挥别 Stack Overflow 了。

上面他们就嘿嘿看一看 howdoi 能做点甚么,加装了后,他们就能间接输出这种的指示,比如说:

howdoipython requests upload file

它给我的回到结论是这种:

files = {upload_file:open(file.txt,rb)}values= {DB:photcat,OUT:csv,SHORT:short}r = requests.post(url, files=files, data=values)

比如说我想搜 python 是不是发送 POST 请求,输出这种的指示:

howdoipython requests post

回到结论是这种:

>>>importrequests>>> r = requests.post(http://httpbin.org/post, json={"key":"value"})>>> r.status_code200>>> r.json{args: {},data:{"key": "value"},files: {},form: {},headers: {Accept:*/*,Accept-Encoding:gzip, deflate,Connection:close,Content-Length:16,Content-Type:application/json,Host:httpbin.org,User-Agent:python-requests/2.4.3 CPython/3.4.0,X-Request-Id:xx-xx-xx},json: {key:value},origin:x.x.x.x,url:http://httpbin.org/post}

看起来还不错对不对。

比如说我想搜 Python 里头是不是把 timestamp 转 datetime,输出这种的指示:

howdoipython timestamp to datetime

回到结论是这种:

fromdatetimeimportdatetimets=int("1284101485")if you encounter a "year is out of range" error the timestampmay be in milliseconds, try `ts /= 1000` in that caseprint(datetime.utcfromtimestamp(ts).strftime(%Y-%m-%d %H:%M:%S))

那 Java 它会吗?试一试:

howdoijava timestamp to datetime

回到结论是这种:

Timestampstamp=newTimestamp(System.currentTimeMillis);Datedate =newDate(stamp.getTime);System.out.println(date);

有点牛逼啊,搜啥标准答案都有,准确率还蛮高。

以上是是不是实现的?没错,是借助于 howdoi 这个项目。

howdoi

那么这个 howdoi 究竟是个甚么?他们 GitHub 上就能找出,镜像地址为:https://github.com/gleitz/howdoi。

看下简介:

Are you a hack programmer? Do you find yourself constantly Googling for how to do basic programming tasks?

Suppose you want to know how to format a date in bash. Why open your browser and read through blogs (risking major distraction) when you can simply stay in the console and ask howdoi.

意思是说,假如你想搜一些程式设计相关的化解方案,他们能不必再去开应用程序,接着再去读文档格式或者博客,你能通过 howdoi 就能间接得到标准答案。

操作是上面他们讲的。

howdoi 是两个 Python 项目,他们能 pip 指示加装:

pip3install howdoi

假如是 Mac 的话,推荐采用 brew 来加装:

brewinstall howdoi

加装完了就能采用 howdoi 指示了。

完整用法如下表所示:

usage: howdoi.py [-h] [-p POS] [-a] [-l] [-c] [-j] [-n NUM_ANSWERS] [-C] [-v] [-e ENGINE] QUERY [QUERY ...]instant coding answers via the command linepositional arguments:QUERY the question to answeroptional arguments:-h,--help showthishelp messageandexit-p POS, --pos POSselectanswerinspecified position (default:1)-a, --all display the full text of the answer-l, --link display only the answer link-c, --color enable colorized output-j, --json-outputreturnanswersinraw json format, to prettyprinttryhowdoi pretty print json command line-n NUM_ANSWERS, --num-answers NUM_ANSWERSnumber of answers toreturn-C, --clear-cache clear the cache-v, --version displays the current version of howdoi-e ENGINE, --engine ENGINE change search engineforthisquery only.Currentlysupported engines: google (default), bing, duckduckgo.

但通常来说就按照前文所演示的间接输出难题就行了。

比如说看一看怎样在 Python 中打印 traceback,这么搜:

howdoiprintstack trace python

结论如下表所示:

importtracebacktry:raiseTypeError("Oups!")exceptException,err:try:raiseTypeError("Again !?!")except:passtraceback.print_exc

不错不错。

原理

这时候我们可能将就有疑问了,这到底是是不是实现的?为甚么还能这么精准。

其实看下源码就晓得了,他们稍微扒下看一看,其实源码就都在这儿了:https://github.com/gleitz/howdoi/blob/master/howdoi/howdoi.py。

先看一看一上来定义了这么多:

SUPPORTED_SEARCH_ENGINES = (google,bing,duckduckgo)URL = os.getenv(HOWDOI_URL)orstackoverflow.comUSER_AGENTS = (Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:11.0) Gecko/20100101 Firefox/11.0,Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100 101 Firefox/22.0,Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0,(Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.5 (KHTML, like Gecko)Chrome/19.0.1084.46 Safari/536.5),(Mozilla/5.0 (Windows; Windows NT 6.1) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.46Safari/536.5), )SEARCH_URLS = {bing: SCHEME +www.bing.com/search?q=site:{0}%20{1}&hl=en,google: SCHEME +www.google.com/search?q=site:{0}%20{1}&hl=en,duckduckgo: SCHEME +duckduckgo.com/?q=site:{0}%20{1}&t=hj&ia=web}

貌似他们就懂了甚么,这些标准答案是从搜索引擎得来的。

接着他们再扒一扒,又看到两个关键的 _get_result 方法,定义如下表所示:

def_get_result(url):try:returnhowdoi_session.get(url, headers={User-Agent: _random_choice(USER_AGENTS)},proxies=get_proxies,verify=VERIFY_SSL_CERTIFICATE).textexceptrequests.exceptions.SSLErrorase:_print_err(Encountered an SSL Error. Try using HTTP instead ofHTTPS by setting the environment variable "HOWDOI_DISABLE_SSL".\n)raisee

看到了吧,这儿是一次 requests 发起了 get 请求,那么这个 url 究竟是是不是来的呢?他们再顺着找下调用 _get_result 方法的地方:

def_get_links(query):search_engine = os.getenv(HOWDOI_SEARCH_ENGINE,google)search_url = _get_search_url(search_engine)result = _get_result(search_url.format(URL, url_quote(query)))if_is_blocked(result):_print_err(Unable to find an answer because the search engine temporarily blocked the request. Please wait a few minutes or select a different search engine.)raiseBlockError("Temporary block by search engine")html = pq(result)return_extract_links(html, search_engine)

这儿是 _get_links 方法调用了 _get_result 方法,是 search_url 的格式化结论,传入了 URL 还有 query,其中 search_url 是 _get_search_url 方法传入了 search_engine,search_engine 是通过环境变量 HOWDOI_SEARCH_ENGINE 获取的,默认是 google。

好,那顺着再看一看 _get_search_url 的实现,定义如下表所示:

def_get_search_url(search_engine):returnSEARCH_URLS.get(search_engine,SEARCH_URLS[google])

很明显了,是 SEARCH_URLS 里头定义的,所以最后,他们就能得到如下表所示的搜索 URL:

www.google.com/search?q=site:stackoverflow.com%20{query}&hl=en

这儿 query 是他们搜索的内容,比如说搜索 print stack trace python,构造的 URL 是:

https://www.google.com/search?q=site:stackoverflow.com%20print%20stack%20trace%20python&hl=en

他们访问呵呵是这种的结论:

其实这是借助 Google 搜索了 Stack Overflow 的内容,搜索了 python 关键字的内容。

接着 howdoi 是提取了其中的 TOP 标准答案,接着解析呵呵回到即可。

彩蛋

既然 howdoi 这么吊,他们来问它两个困扰我们多年的难题吧:

howdoi how tofindgirlfriend

回答:

Sorry, couldnt find anyhelpwiththat topic

甚么?这都不会!摔!

来源:崔庆才 | 静觅

未经允许不得转载:有用的涨知识网 » “抄”标识符,非但不必上Google分页了
分享到: 更多 (0)

有用的涨知识网 带给你想要内容

联系我们