Python爬蟲第一天

1.安裝BeautifulSoup庫(第三方庫,簡化正則,目前還未體會到其應用優勢~~) 

 

 2.Test1:獲取url網頁信息

import urllib.request
response = urllib.request.urlopen('http://python.org/')
result = response.read().decode('utf-8')
print(result)

3.Test2:提取url網頁中包含的超級鏈接/網址

import urllib.request
import re  #re庫用於正則表達式

response = urllib.request.urlopen('http://www.jd.com')
text = response.read().decode('UTF-8')
print(text)
linkre = re.compile('href=\"(.+?)\"')  #編輯正則模型
for x in linkre.findall(text):
    if 'http' in x:
        print('新增地址-->'+x)

4.正則

# pattern = re.compile('正則') 匹配所有
# pettern = re.match('正則') 開始匹配,匹配一次
# pettern = re.research('正則') 中間匹配,匹配一次

 

 

發佈了27 篇原創文章 · 獲贊 2 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章