语言爬虫字段为空_我为什么建议前端将Python 作为第二语言?
前言
本文的文字及圖片來(lái)源于網(wǎng)絡(luò),僅供學(xué)習(xí)、交流使用,不具有任何商業(yè)用途,版權(quán)歸原作者所有,如有問(wèn)題請(qǐng)及時(shí)聯(lián)系我們以作處理。
作者: 前端勸退師
PS:如有需要Python學(xué)習(xí)資料的小伙伴可以加點(diǎn)擊下方鏈接自行獲取
http://note.youdao.com/noteshare?id=3054cce4add8a909e784ad934f956cef
1. Python和ES6語(yǔ)法差別
基本類型
值得注意的是,盡管兩者都是動(dòng)態(tài)類型,但python連接時(shí)并不會(huì)自動(dòng)轉(zhuǎn)換類型。
// JavaScript let coerced = 1; let concatenated = coerced + 'string'; # Python not_coerced = 1 concatenated = not_coerced + 'string'直接報(bào)錯(cuò):TypeError: cannot concatenate 'str' and 'int' objects
只有提前把num轉(zhuǎn)換為字符串類型才能正確運(yùn)行
# Python not_coerced = 1 concatenated = str(not_coerced) + 'string'2. Functions ormethods?
在JavaScript和Python中,函數(shù)和條件的結(jié)構(gòu)極為相似。例如:
// JavaScript function drSeuss(catInTheHat, thing1, thing2) {if (catInTheHat == true &&thing1 == true &&thing2 == true) {console.log('is cray');} else if (catInTheHat != true) {console.log('boring');} else {console.log('so boring');} } # Python def dr_seuss(cat_in_the_hat, thing1, thing2):if cat_in_the_hat == True andthing2 == True andthing2 == True:print 'is cray'elif cat_in_the_hat != True:print 'boring'else:print 'so boring'但在JavaScript中,“methods”的通俗定義是指語(yǔ)言規(guī)范中內(nèi)置的方法,例如:Function.prototype.apply()。 在MDN上有對(duì)二者的解釋: 在大多數(shù)方面,Functions和methods相同,但有兩個(gè)主要區(qū)別:
- methods可以被隱式傳遞到調(diào)用該methods的對(duì)象上。
- methods能夠?qū)︻愔邪臄?shù)據(jù)進(jìn)行操作。
然鵝,在JavaScript中,“類”只是語(yǔ)法糖的存在,稍后我們?cè)龠M(jìn)行對(duì)比。
3. 模板字符串
在模板字符串上,JavaScript之前是領(lǐng)先于python的。
// JavaScript let exclamation = 'Whoa!'; let sentence = `They are really similar to Python.`;console.log(`Template Literals: ${exclamation} ${sentence}`); # python print '打印: {} {}'.format('Whoa.', 'Quite!') # 打印: Yup. Quite!{}充當(dāng)占位符。 這種語(yǔ)法被詬病頗多,于是在后來(lái)的Python3.6版本中,又提供了一種字符串格式化語(yǔ)法——f-strings。
直接對(duì)比:
name = "Tom" age = 3 print(f"他叫 {name}, {age} 歲") # "他叫Tom, 3 歲"4. 參數(shù)默認(rèn)值
JavaScript再次完美“借鑒”P(pán)ython:
// JavaScript function nom(food="ice cream") {console.log(`Time to eat ${food}`); }nom();// Time to eat ice cream# Python def nom(food="ice cream"):print 'Time to eat {}'.format(food)nom() # Time to eat ice cream5. 其余參數(shù)和* args
Rest參數(shù)語(yǔ)法,使我們可以將不定數(shù)量的參數(shù)表示為數(shù)組,傳入函數(shù)中。
- 在Python中,它們稱為* args
- 在JavaScript中...xxx就表示為其余參數(shù)。
6. Classes:類
眾所周知,ES6類實(shí)際上是語(yǔ)法糖。 Python具有內(nèi)置的類,可以快速,輕松地進(jìn)行面向?qū)ο蟮木幊獭?/p>
而JavaScript原型鏈繼承,是每個(gè)前端的必須課。
// JavaScript class Mammal {constructor() {this.neocortex = true;} }class Cat extends Mammal {constructor(name, years) {super();this.name = name;this.years = years;}eat(food) {console.log('nom ' + food);} }# Python class Mammal(object):neo_cortex = Trueclass Cat(Mammal):def __init__(self, name, years):self.name = nameself.years = yearsdef eat(food):print 'nom %s' % (food)fry_cat = Cat('Fry', 7) fry_cat.eat('steak')心而論,Python的寫(xiě)法更優(yōu)雅。。。
7. Modules and import:模塊
ES6的模塊語(yǔ)言借鑒于python,卻優(yōu)秀于它。兩者之間有一些區(qū)別:
- JavaScript導(dǎo)入是靜態(tài)的;Python是動(dòng)態(tài)的。
- JavaScript模塊必須顯式導(dǎo)出。在Python中,所有模塊均可導(dǎo)入。
- JavaScript具有默認(rèn)導(dǎo)出的概念。Python沒(méi)有。
1. 導(dǎo)入分模塊
在javascript中,我們想導(dǎo)入分模塊直接解構(gòu)賦值就可以了
// javascript import { myvar, myfunc } from "./mymodule"; console.log(myvar); myfunc();而在python,其語(yǔ)義則相反:
# python from mymodule import myvar, myfunc print myvar myfunc()2. 導(dǎo)出空函數(shù)
如何想導(dǎo)出一段空函數(shù),python需要用到“pass“關(guān)鍵詞占位,避免運(yùn)行出錯(cuò)。 mymodule.py:
# python def myfunc(): pass// javascript export function myfunc() {}前端如何優(yōu)雅學(xué)會(huì)Python?
許多前端對(duì)Python的熱情始于好奇,終于停滯。
距離實(shí)干做開(kāi)發(fā)有技術(shù)差距,也無(wú)人指點(diǎn)提帶,也不知當(dāng)下水平能干嘛?就在這樣的疑惑循環(huán)中,編程技能止步不前,而爬蟲(chóng)是最好的進(jìn)階方向之一。
網(wǎng)絡(luò)爬蟲(chóng)是Python比較常用的一個(gè)場(chǎng)景,國(guó)際上,google在早期大量地使用Python語(yǔ)言作為網(wǎng)絡(luò)爬蟲(chóng)的基礎(chǔ),帶動(dòng)了整個(gè)Python語(yǔ)言的應(yīng)用發(fā)展。
就我個(gè)人發(fā)展而已,我也十分推薦以爬蟲(chóng)為應(yīng)用入門,原因有幾項(xiàng):
- 爬蟲(chóng)是針對(duì)web頁(yè)面的一種應(yīng)用技術(shù),前端可以無(wú)痛銜接很多知識(shí)。
- 爬蟲(chóng)的第一步是獲取頁(yè)面源碼,然后做信息抽取。其中針對(duì)dome節(jié)點(diǎn)的class/id選擇,前端無(wú)需再度學(xué)習(xí)。
- 爬蟲(chóng)中的虛擬登錄及Selenium,可以提升前端對(duì)于自動(dòng)化測(cè)試的理解。
- 爬蟲(chóng)的最終形態(tài)是搜索引擎,當(dāng)中的SEO是每個(gè)前端都需要關(guān)注的點(diǎn)兒。
- 在了解搜索引擎爬蟲(chóng)的過(guò)程中,前端可以搞清楚服務(wù)端渲染SSR和單頁(yè)應(yīng)用CSR的不同作用。
爬蟲(chóng)分兩種方式:面向頁(yè)面和面向接口
- 面向頁(yè)面,前端自然輕車熟路。
- 面向接口,需要了解到如何用抓包軟件(Fiddler/Charles)。
- 在這過(guò)程中,又能學(xué)會(huì)一項(xiàng)技能 - 抓包。以后不用再看著Network傻傻刷新了。
始于爬蟲(chóng),卻不止于爬蟲(chóng):
爬蟲(chóng)—> 數(shù)據(jù)清洗 -> 數(shù)據(jù)庫(kù)操作 -> 數(shù)據(jù)清洗 -> 數(shù)據(jù)挖掘 -> 數(shù)據(jù)分析 ...
這一條鏈下去,你可以學(xué)到非常非常多的知識(shí):
Scrapy爬蟲(chóng)框架,Redis分布式事務(wù),數(shù)據(jù)處理Pandas,自然語(yǔ)言分析NLP,完整實(shí)現(xiàn)數(shù)據(jù)可視化等等....
關(guān)于語(yǔ)言的討論,我非常贊同李兵老師的這段話:
3. 潘石屹都在學(xué)Python
.
總結(jié)
以上是生活随笔為你收集整理的语言爬虫字段为空_我为什么建议前端将Python 作为第二语言?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 余额的钱怎么免费转到银行卡
- 下一篇: svn版本库浏览器_在SVN版本库浏览器