大神是如何走過來的,六個(gè)項(xiàng)目代碼了解一下(上)

1、抓取知乎圖片,只用30行代碼:# 視頻資料分享 QQ群 519970686?

importre? ? ? ? ? ?

from?seleniumimportwebdriver

importtime

importurllib.request

driver?=?webdriver.Chrome()

driver.maximize_window()

driver.get("https://www.zhihu.com/question/29134042")

i?=0

whilei?<10:

driver.execute_script("window.scrollTo(0,?document.body.scrollHeight);")

time.sleep(2)

try:

driver.find_element_by_css_selector('button.QuestionMainAction').click()

print("page"+?str(i))

time.sleep(1)

except:

break

result_raw?=?driver.page_source

content_list?=?re.findall("img?src="(.+?)"?",?str(result_raw))

n?=0

whilen?<?len(content_list):

i?=?time.time()

local?=?(r"%s.jpg"%?(i))

urllib.request.urlretrieve(content_list[n],?local)

print("編號:"+?str(i))

n?=?n?+1

2、沒事閑的時(shí)候,聽兩個(gè)聊天機(jī)器人互相聊天:# 視頻資料分享 QQ群 519970686

fromtimeimportsleep

import?requests

s=?input("請主人輸入話題:")

whileTrue:

resp?=?requests.post("http://www.tuling123.com/openapi/api",data={"key":"4fede3c4384846b9a7d0456a5e1e2943","info":s,?})

resp?=?resp.json()

sleep(1)

print('小魚:',?resp['text'])

s=?resp['text']

resp?=?requests.get("http://api.qingyunke.com/api.php",?{'key':'free','appid':0,'msg':s})

resp.encoding?='utf8'

resp?=?resp.json()

sleep(1)

print('菲菲:',?resp['content'])

網(wǎng)上還有一個(gè)據(jù)說智商比較高的小i機(jī)器人,用爬蟲的功能來實(shí)現(xiàn)一下:

import?urllib.request

import?re

whileTrue:

x=?input("主人:")

x=?urllib.parse.quote(x)

link=?urllib.request.urlopen(

"http://nlp.xiaoi.com/robot/webrobot?&callback=__webrobot_processMsg&data=%7B%22sessionId%22%3A%22ff725c236e5245a3ac825b2dd88a7501%22%2C%22robotId%22%3A%22webbot%22%2C%22userId%22%3A%227cd29df3450745fbbdcf1a462e6c58e6%22%2C%22body%22%3A%7B%22content%22%3A%22"+x+"%22%7D%2C%22type%22%3A%22txt%22%7D")

html_doc?=?link.read().decode()

reply_list?=?re.findall(r'"content":"(.+?)\r\n"',?html_doc)

print("小i:"+?reply_list[-1])

3、AI分析唐詩的作者是李白還是杜甫:# 視頻資料分享 QQ群 519970686

importjieba

fromnltk.classifyimportNaiveBayesClassifier

#?需要提前把李白的詩收集一下,放在libai.txt文本中。

text1?=?open(r"libai.txt","rb").read()

list1?=?jieba.cut(text1)

result1?="?".join(list1)

#?需要提前把杜甫的詩收集一下,放在dufu.txt文本中。

text2?=?open(r"dufu.txt","rb").read()

list2?=?jieba.cut(text2)

result2?="?".join(list2)

#?數(shù)據(jù)準(zhǔn)備

libai?=?result1

dufu?=?result2

#?特征提取

defword_feats(words):

returndict([(word,True)forwordinwords])

libai_features?=?[(word_feats(lb),'lb')forlbinlibai]

dufu_features?=?[(word_feats(df),'df')fordfindufu]

train_set?=?libai_features?+?dufu_features

#?訓(xùn)練決策

classifier?=?NaiveBayesClassifier.train(train_set)

#?分析測試

sentence?=?input("請輸入一句你喜歡的詩:")

print("")

seg_list?=?jieba.cut(sentence)

result1?="?".join(seg_list)

words?=?result1.split("?")

#?統(tǒng)計(jì)結(jié)果

lb?=0

df?=0

forwordinwords:

classResult?=?classifier.classify(word_feats(word))

ifclassResult?=='lb':

lb?=?lb?+1

ifclassResult?=='df':

df?=?df?+1

#?呈現(xiàn)比例

x?=?float(str(float(lb)?/?len(words)))

y?=?float(str(float(df)?/?len(words)))

print('李白的可能性:%.2f%%'%?(x?*100))

print('杜甫的可能性:%.2f%%'%?(y?*100))

? ? ? ? ? ?真心想學(xué)習(xí)Python的朋友,群內(nèi)有視頻資料分享,歡迎一起加入Python的浪潮,逐浪前行

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容