除了用jupyter的檔案總管介面管理檔案,也可以利用Terminal執行Unix語法做檔案管理。
cd <path>
:進入位置ls <path>
:列出位置下的檔案mkdir <folder_name>
:新增檔案夾cp <now_file_path> <new_file_path>
:複製檔案mv <now_file_path> <new_file_path>
:移動檔案rm <now_file_path>
:刪除檔案
-rm -r <now_path>
:刪除檔案夾pwd
:顯示現在的位置GET
from lxml import etree
import requests
url = "http://blog.marsw.tw"
response = requests.get(url)
html = response.text
fileout = open("test.html","w")
fileout.write(html)
fileout.close()
page = etree.HTML(html)
first_article_tags = page.xpath("//footer/a/text()")[0]
print (first_article_tags)
for article_title in page.xpath("//h5/a/text()"):
print (article_title)
旅遊-日本 2014。09~關西9天滾出趣體驗 2014。05 日本 ~ Day9 旅程的最後~滾出趣精神、令人屏息的司馬遼太郎紀念館 2014。05 日本 ~ 滾出趣(調查11) 道頓堀的街頭運動 2014。05 日本 ~ Day8 鞍馬天氣晴、貴船川床流水涼麵、京都風情 2014。05 日本 ~ 高瀬川、鴨川、祇園之美
from lxml import etree
只從 lxml 工具箱,裝備 etree 這個工具
import requests
裝備 requests 工具箱
url = "http://blog.marsw.tw"
將 "http://blog.marsw.tw"
這個 <字串 string>
儲存到 我們命名的 url
<變數 variable>
response = requests.get(url)
使用 requests
工具箱的 get
工具,
這個工具能幫我們抓下網址為 url
的網頁(傳遞資料的方法為 GET
)上的資料,
把這些資料存到 我們命名的 response
<變數 variable>
html = response.text
將 response
屬於 text
的資料,也就是網頁原始碼(response是用get抓下來的網頁資料)
存到 我們命名的 html
<變數 variable>
工具箱、工具 在 Python裡面的專有名詞分別是
「模組 module」和 「函式 function / 類別 class」,
比較複雜一點所以今天不會特別說明。
我們先來看看 <變數 variable>以及 <字串 string>
url = "http://blog.marsw.tw"
response = requests.get(url)
html = response.text
url
、response
、html
都是自行命名的變數import
, for
, in
, str
...)xyz = "http://blog.marsw.tw"
vs.url = "http://blog.marsw.tw"
"
或是單引號'
,將字串包住%s
」my_string = "PyLadies Taiwan"
my_string2 = ""
my_string2 = my_string2 + "Py" + "Ladies"
my_string3 = "Py%s Taiwan"%("Ladies")
print (my_string)
print (my_string2)
print (my_string3)
PyLadies Taiwan PyLadies PyLadies Taiwan
%s
應用情境¶# 產生各股票網址(股票代號是在網址中,不是在尾端)
url = "http://www.wantgoo.com/stock/%s?searchType=stocks"%(2330)
print (url)
url = "http://www.wantgoo.com/stock/%s?searchType=stocks"%(2371)
print (url)
http://www.wantgoo.com/stock/2330?searchType=stocks http://www.wantgoo.com/stock/2371?searchType=stocks
# 產生facebook粉絲頁資訊(想讓資訊好看,不想用+來處理)
url = "https://www.facebook.com/%s/%s/"%("pyladies.tw","about")
print (url)
url = "https://www.facebook.com/%s/%s/"%("pyladies.tw","photos")
print (url)
https://www.facebook.com/pyladies.tw/about/ https://www.facebook.com/pyladies.tw/photos/
不使用格式化字串,直接用字串相加就會變成:
url = "https://www.facebook.com/"+"pyladies.tw"+"/"+"about"+"/"
而不用變數 url
儲存,直接用print
印出,就會像以下看起來很雜亂的程式碼:
print ("https://www.facebook.com/"+"pyladies.tw"+"/"+"about"+"/")
fileout = open("test.html","w")
open
是讓我們開啟一個檔案的工具,而這個檔案名稱我們叫做test.html
,
而這個檔案是用來「寫入資料」,因此要加上w
,不加的話會是用來「讀取檔案」。
我們把這個工具存在 fileout
這個我們命名的變數中。
fileout.write(html)
然後使用 write
功能將 存在變數 html
的資料寫入(會寫在 test.html
中)
fileout.close()
最後,怕同時間有其他人/程式也一起使用這個檔案,
會造成檔案的內容被影響,所以我們在程式中以close
功能關閉這個檔案,
至少現階段這個程式不會再影響檔案了。
article = "Bubble tea represents the 'QQ' food texture that Taiwanese love. The phrase refers to something that is especially chewy, like the tapioca balls that form the 'bubbles' in bubble tea. It's said this unusual drink was invented out of boredom. Chun Shui Tang and Hanlin Tea Room both claim to have invented bubble tea by combining sweetened tapioca pudding (a popular Taiwanese dessert) with tea. Regardless of which shop did it first, today the city is filled with bubble tea joints. Variations on the theme include taro-flavored tea, jasmine tea and coffee, served cold or hot."
fileout = open("my_article.txt","w")
fileout.write(article)
fileout.close()
為什麼要用fileout
這個變數來儲存open
的檔案,
因為我們是用w
來寫檔案,遇到一模一樣名字的檔案,會直接把原有的資料洗掉,
所以如果寫成:
open('test.html','w').write(html)
open('test.html','w').close()
第二次用 open
以 w
開啟 test.html
檔案,
就把第一次 write
的內容洗掉了,
因此我們使用變數,讓我們開一次檔案就好。
現在大家知道了
GET
的網頁輸入程式碼,按下介面上的 或是用快捷鍵Ctrl+Enter、Shift+Enter編譯執行
from lxml import etree
import requests
url = "http://blog.marsw.tw"
response = requests.get(url)
html = response.text
fileout = open("test.html","w")
fileout.write(html)
fileout.close()
test.html
的檔案¶url
來抓下不同的網頁看看吧!¶from lxml import etree
import requests
url = "http://blog.marsw.tw"
response = requests.get(url)
html = response.text
fileout = open("test.html","w")
fileout.write(html)
fileout.close()
page = etree.HTML(html)
把變數html
儲存的資料(網頁抓下來的原始碼),
以 工具etree
的HTML
功能,轉換成「XPath
的節點(node)型態」,
並儲存到變數page
中。
範例程式第一行宣告的 lxml 工具箱的 etree 工具,終於要用到啦!
from lxml import etree
<a href="">連結文字</a>
<img src=""/>
<h1>
~<h6>
<br>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>Subtitle</h1>
<a href="http://tw.pyladies.com/">PyLadies Website</a>
<p>
This is a paragraph <br>
<a href="http://www.meetup.com/PyLadiesTW/">PyLadies Meetup</a> <br>
<a href="https://www.facebook.com/pyladies.tw">PyLadies FB</a> <br>
</p>
<img src="http://tw.pyladies.com/img/logo2.png" width="99px"/>
</body>
</html>
first_article_tags = page.xpath("//footer/a/text()")[0]
前面的程式碼,我們已經讓變數page
是「XPath
的節點(node)型態」,
這邊的程式碼是以變數page
用xpath
找尋
整個文件中 //
, 所有 footer
標籤,且小孩是 a
標籤的文字屬性 text()
,
而我們只取「第1個」結果,存到變數 first_article_tags
中。
print (first_article_tags)
將 變數 first_article_tags
儲存的資料,印到螢幕上。
from lxml import etree
# 這邊先不使用request去抓,直接將原始碼存到 變數 html 中,方便大家理解
# 遇到長篇文章有換行的存在,可用「三個」雙引號或單引號
html = """
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>Subtitle</h1>
<a href="http://tw.pyladies.com/">PyLadies Website</a>
<p>
This is a paragraph <br>
<a href="http://www.meetup.com/PyLadiesTW/">PyLadies Meetup</a> <br>
<a href="https://www.facebook.com/pyladies.tw">PyLadies FB</a> <br>
</p>
<img src="http://tw.pyladies.com/img/logo2.png" width="99px"/>
</body>
</html>
"""
page = etree.HTML(html)
link_text_list = page.xpath("//a/text()")
link_text_p_list = page.xpath("//p/a/text()")
link_list = page.xpath("//a/@href")
print (link_text_list)
print (link_text_p_list)
print (link_list)
['PyLadies Website', 'PyLadies Meetup', 'PyLadies FB'] ['PyLadies Meetup', 'PyLadies FB'] ['http://tw.pyladies.com/', 'http://www.meetup.com/PyLadiesTW/', 'https://www.facebook.com/pyladies.tw']
整個網頁中,我們可以看到其實有很多個連結(a標籤
),
這種一次存很多資料的變數型別,我們叫做「串列(list)」
append()
my_list = ["a",2016,5566,"PyLadies"]
my_list2=[]
my_list2.append(2016)
my_list2.append("abc")
print (my_list)
print (my_list2)
['a', 2016, 5566, 'PyLadies'] [2016, 'abc']
my_list = ["a",2016,5566,"PyLadies",2016,2016.0]
print ("The 1th element of my_list = ",my_list[0])
print ("The 4th element of my_list = ",my_list[3])
print ("The last element of my_list = ",my_list[-1])
print ("The second-last element of my_list = ",my_list[-2])
The 1th element of my_list = a The 4th element of my_list = PyLadies The last element of my_list = 2016.0 The second-last element of my_list = 2016
my_string = "PyLadies Taiwan"
print ("The 1st element of my_string = ",my_string[0])
print ("The 8th element of my_string = ",my_string[7])
print ("The last element of my_string = ",my_string[-1])
The 1st element of my_string = P The 8th element of my_string = s The last element of my_string = n
len
:計算長度count
:計算某個元素出現過幾次my_string = "PyLadies Taiwan"
print ("Length of my_string = ",len(my_string))
print ("The time 'a' appears in my_string = ",my_string.count('a'))
Length of my_string = 15 The time 'a' appears in my_string = 3
my_list = ["a",2016,5566,"PyLadies",2016,2016.0]
print ("Length of my_list = ",len(my_list))
print ("The time '2016' appears in my_string = ",my_list.count(2016))
Length of my_list = 6 The time '2016' appears in my_string = 3
from lxml import etree
html = """
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>Subtitle</h1>
<a href="http://tw.pyladies.com/">PyLadies Website</a>
<p>
This is a paragraph <br>
<a href="http://www.meetup.com/PyLadiesTW/">PyLadies Meetup</a> <br>
<a href="https://www.facebook.com/pyladies.tw">PyLadies FB</a> <br>
</p>
<img src="http://tw.pyladies.com/img/logo2.png" width="99px"/>
</body>
</html>
"""
page = etree.HTML(html)
link_text_list = page.xpath("//a/text()")
print (link_text_list)
print ("網頁中共有",len(link_text_list),"個連結")
['PyLadies Website', 'PyLadies Meetup', 'PyLadies FB'] 網頁中共有 3 個連結
for article_title in page.xpath("//h5/a/text()"):
print (article_title)
取出 整個文件中 //
, 所有 h5
標籤,且小孩是 a
標籤的文字屬性 text()
的結果(串列),
把他們一個個取出,放到變數article_title
中,並把它印出來。
my_list = ["a",2016,5566,"PyLadies"]
print ("a" in my_list)
print (5678 in my_list)
True False
my_string = "PyLadies Taiwan"
print ("a" in my_string)
print ("Py" in my_string)
print ("Python" in my_string)
True True False
my_list = ["a",2016,5566,"PyLadies"]
for element in my_list:
print (element)
a 2016 5566 PyLadies
PyLadies Website
PyLadies Meetup
PyLadies FB
//*[@id="Blog1"]/div[1]/div[1]/div/div[1]/article/header/h5/a
//h5/a
from lxml import etree
import requests
url = "http://blog.marsw.tw"
response = requests.get(url)
html = response.text
page = etree.HTML(html)
for img_src in page.xpath("//img/@src"):
# 抓取圖片
img_response = requests.get(img_src)
img = img_response.content
filename=img_src.split("/")[-1]
filepath="tmp/"+filename
fileout = open(filepath,"wb")
fileout.write(img)
content
,而不是像之前抓網頁的時候是用 text
,是取得bytes
型式的內容bytes
型式的內容,因此用的是 wb
,而不是 w
字串串列 = 原字串.split(子字串)
:將「原字串」以「子字串」切割為「字串串列」 my_string = "PyLadies Taiwan"
print (my_string.split(" "))
print (my_string.split(" ")[0])
print (my_string.split(" ")[-1])
['PyLadies', 'Taiwan'] PyLadies Taiwan
img_src = "http://1.bp.blogspot.com/-lP9M5nJ-kb0/U3nAOYRLCAI/AAAAAAAAUaw/1SbrOZBwz3g/s1600/2014-05-01%2B22.26.27.jpg"
print (img_src.split("/"))
print (img_src.split("/")[-1])
['http:', '', '1.bp.blogspot.com', '-lP9M5nJ-kb0', 'U3nAOYRLCAI', 'AAAAAAAAUaw', '1SbrOZBwz3g', 's1600', '2014-05-01%2B22.26.27.jpg'] 2014-05-01%2B22.26.27.jpg
//*[@id="Blog1"]/div[1]/div[1]/div/div[1]/article/header/h5/a
PyLadies受邀課程
Learn Python The Hard Way:有詳細的說明及簡單範例(Python 2.x)
Codecademy - Python:透過大量的互動練習來熟悉Python的語法(Python 2.x)