什么软件可以搜索python答案_【python学习手记】网课精灵~调用爬虫搜索网课答案~...
前幾天刷網(wǎng)課,但是搜索答案的“正解網(wǎng)”需要操作的步驟太多了...從復制..到搜索,然后點擊搜索結(jié)果,再往下翻才能看到答案,而且正解網(wǎng)的搜索引擎也有問題,經(jīng)常搜不到,又要重新百度搜索一遍...嚴重降低了查答案的效率。那么有沒有什么辦法一氣呵成提高效率呢?
這時候就需要...網(wǎng)課精靈啦~
網(wǎng)課精靈是一款用python編寫的,可運行于服務器或者個人電腦的..網(wǎng)課答案搜索軟件!
#!/usr/bin/python
#encoding:utf-8
import urllib2
import re
import json
import sys
import urllib
import httplib
reload(sys)
sys.setdefaultencoding('utf-8')
def shorturl(urlinput):
conn=httplib.HTTPConnection('dwz.cn')
params=urllib.urlencode({'url':"%s"%urlinput})
conn.request('POST','/create.php',headers={"Content-Type":"application/x-www-form-urlencoded"},body=params)
result=conn.getresponse()
resultContent=result.read()
dic_json=json.loads(resultContent)
#print dic_json
try:
shorturlans="%s"%dic_json['tinyurl']
outurl=shorturlans
except:
shorturlans="%s"%dic_json['err_msg']
return shorturlans
def fuckchaoxin(quest):
url=quest
url="https://www.zhengjie.com/s?type=question&q="+str(urllib2.quote(url))
response = urllib2.urlopen(url)
get= response.read()
title=re.findall(r"a href='/question/(.*?)'",get)
url="https://www.zhengjie.com/question/"+title[5]
response = urllib2.urlopen(url)
get=response.read()
try:
try:
title=re.findall(r"class='resource_title'>(.*?)",get)
out="問題:"+title[0]
title=re.findall(r"正確答案
- (.*?)",get)
out=out+"\n答案:"+title[0]
except:
title=re.findall(r"class='resource_title'>(.*?)",get)
out="問題:"+title[0]
title=re.findall(r"正確答案
(.*?)
",get)out=out+"\n答案"+title[0]
except:
out="找不到結(jié)果"
out=out+"結(jié)果不對?看看百度的結(jié)果吧:"+shorturl("https://www.baidu.com/s?wd="+str(urllib2.quote(aow)))
以上是網(wǎng)課精靈的核心代碼,需要用到的時候,只要output=fuckchaoxing("網(wǎng)課問題關鍵字")就能直接獲得網(wǎng)課的答案,如果搜索結(jié)果錯誤,也可以通過短網(wǎng)址查看百度搜索該題目的結(jié)果~
在正解網(wǎng)中搜索一個問題后,打開的url為https://www.zhengjie.com/s?type=question&q=問題的URL字符
所以我們通過換算問題的URL字符來獲取了搜索結(jié)果的頁面并讀取,查詢html可以發(fā)現(xiàn),所有的答案頁面都是"https://www.zhengjie.com/question/XXXXX"的格式,通常,我們需要的是第一個答案,所以我匹配了獲得的搜索頁面中全部含"/question/"的字段XXXXX,其中第五個含有"/question/"的XXXXX就是我們需要的答案頁面了
接下來讓爬蟲打開https://www.zhengjie.com/question/XXXXX,查詢html,匹配問題和答案的字串進行顯示就可以了~針對搜索結(jié)果的問題,我通過了百度短網(wǎng)址API來縮短了百度搜索該問題的鏈接,這樣即使搜索結(jié)果不對,也能通過一鍵點開百度對應搜索來查詢正確的答案,大幅提高了網(wǎng)課答題的效率。
總結(jié)
以上是生活随笔為你收集整理的什么软件可以搜索python答案_【python学习手记】网课精灵~调用爬虫搜索网课答案~...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: es6 --- Promise.all
- 下一篇: javascript --- 变量提升