前言:今天看到一個(gè)開發(fā)群問有人會(huì)爬蟲嗎,然后突然想試試看,在慕課網(wǎng)上找到一個(gè)爬蟲教程徙邻,跟著邊看做,以下是學(xué)習(xí)記錄畸裳。
1.一些概念性截圖
2.開發(fā)環(huán)境
- Google了以下python開發(fā)用什么編輯器好缰犁,大部分答案說pycharm。去官網(wǎng)上下載了一個(gè)安裝怖糊,需要激活或者試用三十天帅容。搜了下pycharm激活方式,找了一個(gè)注冊(cè)碼安裝成功了伍伤。
- 網(wǎng)頁解析器需要用到beautiful soup 官網(wǎng)下載最新安裝包丰嘉,解壓,終端進(jìn)入目錄嚷缭,執(zhí)行以下命令饮亏。
sudo python setup.py install
- beautiful soup 使用實(shí)例
#coding=utf-8
from bs4 import BeautifulSoup #引入 beautiful soup 包
import re #正則表達(dá)式包
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a class="sister" id="link1">Elsie</a>,
<a class="sister" id="link2">Lacie</a> and
<a class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
soup = BeautifulSoup(html_doc, 'html.parser', from_encoding='utf-8')
print 'get all links'
links = soup.find_all('a')
for link in links:
print link.name, link['href'], link.get_text()
print '獲取 lacie 的鏈接'
link_node = soup.find('a', )
print link_node.name, link_node['href'], link_node.get_text()
print '正則匹配'
link_node1 = soup.find('a', href=re.compile(r"ill"))
print link_node1.name, link_node1['href'], link_node1.get_text()
print '獲取p段落文字'
link_node2 = soup.find('p', class_="story")
print link_node2.name, link_node2.get_text()