Python Web Scraping ———07.31.2017

Three different methods in data-scaring: six.urllib, beautifulsoup, RE-xpath

Just write down what I've learned about web data scraping so that I won't forget everything and start all over next time I need to use the technique.


To work easier with python 2.x, try use lib "six":

from six.moves import urllib

Typical request format would be:

url = ...

hdr = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36', 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'} This depends on your laptop spec

req = urllib.request.Request(url, headers=hdr)

doc = urllib.request.urlopen(req).read() This gives you a file of unicodes

Now, it all comes to the choice among different parsing tools that you like to use, beautifulsoup/regular expression… What I have tried is RE-xpath, RE-pattern matching, and beautifulsoup.?

For RE-xpath:

IP_ADDRESS_PATH = '//td[2]/text()'

PORT_ADDRESS_PATH = '//tr/td[3]/text()'

You need to understand the html file and know how to construct the xpath towards the notes you like to extract. So for the above IP_ADDRESS_PATH, it's actually saying that starting from the root, find the text of all the third td.

IP_list = list(set(re.findall(IP_ADDRESS_PATH, doc)))

Then use the re.findall() method to find all the contents of nodes you want. Set() makes elements unique and list() turns it back to the list.

** Not sure why this wasn't working, but pretty sure the xpath was constructed correctly since it's verified by some html tester.

For RE-pattern match:

prep = re.compile(r"""<tr\s.*>….\n....</tr>""", re.VERBOSE)

\s means a space in the xpath, \n means a return in the xpath, .* means it represents whatever (could be anything). This summarizes the pattern of the specific block that might be repeated for many times and is under your interest.?

proxy_list = prep.findall(doc) ?

proxy_list = list(set(proxy_list))

proxy_list now contains all the block of codes that have the same pattern.

For beautifulsoup:

You still need six.moves urllib to open up the url.

req = urllib.request.Request(url, headers=hdr)

doc = urllib.request.urlopen(req).read()

soup = bs(doc, 'lxml')

So now you've opened up the html file and can start parsing with the beautiful beautifulsoup.?

list1 = [tr.find_all('td') for tr in soup.find_all('tr')]


Okay, three methods that I have learned and tried to parse the html files and extract data. What I ended up doing is with the "bs" and make sure that the method under "bs" is find_all() not findall().

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子唠叛,更是在濱河造成了極大的恐慌,老刑警劉巖祈搜,帶你破解...
    沈念sama閱讀 211,376評論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機框沟,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,126評論 2 385
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來增炭,“玉大人街望,你說我怎么就攤上這事〉芘埽” “怎么了?”我有些...
    開封第一講書人閱讀 156,966評論 0 347
  • 文/不壞的土叔 我叫張陵防症,是天一觀的道長孟辑。 經(jīng)常有香客問我,道長蔫敲,這世上最難降的妖魔是什么饲嗽? 我笑而不...
    開封第一講書人閱讀 56,432評論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮奈嘿,結(jié)果婚禮上貌虾,老公的妹妹穿的比我還像新娘。我一直安慰自己裙犹,他們只是感情好尽狠,可當(dāng)我...
    茶點故事閱讀 65,519評論 6 385
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著叶圃,像睡著了一般袄膏。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上掺冠,一...
    開封第一講書人閱讀 49,792評論 1 290
  • 那天沉馆,我揣著相機與錄音,去河邊找鬼德崭。 笑死斥黑,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的眉厨。 我是一名探鬼主播锌奴,決...
    沈念sama閱讀 38,933評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼憾股!你這毒婦竟也來了缨叫?” 一聲冷哼從身側(cè)響起椭符,我...
    開封第一講書人閱讀 37,701評論 0 266
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎耻姥,沒想到半個月后销钝,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,143評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡琐簇,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,488評論 2 327
  • 正文 我和宋清朗相戀三年蒸健,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片婉商。...
    茶點故事閱讀 38,626評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡似忧,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出丈秩,到底是詐尸還是另有隱情盯捌,我是刑警寧澤,帶...
    沈念sama閱讀 34,292評論 4 329
  • 正文 年R本政府宣布蘑秽,位于F島的核電站饺著,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏肠牲。R本人自食惡果不足惜幼衰,卻給世界環(huán)境...
    茶點故事閱讀 39,896評論 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望缀雳。 院中可真熱鬧渡嚣,春花似錦、人聲如沸肥印。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,742評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽深碱。三九已至裤唠,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間莹痢,已是汗流浹背种蘸。 一陣腳步聲響...
    開封第一講書人閱讀 31,977評論 1 265
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留竞膳,地道東北人航瞭。 一個月前我還...
    沈念sama閱讀 46,324評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像坦辟,于是被迫代替她去往敵國和親刊侯。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,494評論 2 348

推薦閱讀更多精彩內(nèi)容