1. 程式人生 > >Python3.X爬蟲

Python3.X爬蟲

6.4 data 3.6 cal span python3 ogr itl pre

1、Python很有名,但是一直沒在實際項目中用過,今天花30分鐘學習下。
去Python官網https://www.python.org/downloads/

技術分享圖片
2、2.X與3.X版本相差比較大,新手用最新的3.6.4。
3、下載安裝。
4、安裝BeautifulSoup,CMD進入C:\Users\zgj\AppData\Local\Programs\Python\Python36-32\Scripts,運行 pip install bs4.
5、桌面建一個記事本test.py,去網上找例子,註意3.x與2.x語法不一樣下面代碼是3.x下可以使用的。

#!/usr/bin/python  
# -*- coding: utf-8 -*-  
from bs4 import BeautifulSoup import urllib.request url = rhttp://douban.com res = urllib.request.urlopen(url) html = res.read().decode(utf-8) 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 href="http://example.com/elsie" class="sister" id="link1">Elsie</a>, <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p>
""" #獲取BeautifulSoup對象並按標準縮進格式輸出,下面用html,或者html_doc一個是本地,一個是遠程。 soup = BeautifulSoup(html,"html.parser") print(soup.prettify()) print(soup.title)

6、右鍵Edit with IDE,Run、Run Moudle,輸出結果了吧,入門就這麽簡單。

Python3.X爬蟲