1. 程式人生 > >[Python]python爬蟲簡單試用

[Python]python爬蟲簡單試用

.com www pytho request rom open url 使用 開始

一直用的是python3.4版本,所以只用了urllib爬數據,然後使用BeautifulSoup做為分析。

1、首先安裝BeautifulSoup,執行命令如下:

pip install BeautifulSoup4

2、第二步開始寫代碼,就以我的博客為例,其實代碼很簡單

from urllib import request
from bs4 import BeautifulSoup

fp = request.urlopen("http://www.cnblogs.com/youyuan1980/")
html = fp.read()
soup = BeautifulSoup(html, ‘html.parser‘)
for div in soup.find_all(‘a‘,attrs={"class":"postTitle2"}):
    print(div.get(‘href‘))        #顯示a標簽屬性的href
    print(‘text:‘+div.get_text()) #顯示a標簽裏面的text

不用解釋,看看就明白了。

[Python]python爬蟲簡單試用