Python入門簡單的靜態網頁爬蟲2.0 (實現各模塊的具體方法)

    1、URL管理器html

    URL管理器:管理待爬取URL集合和已抓取的URL集合,主要是爲了防止重複和循環抓取。node

    功能分析:添加new_url到待爬取集合中,在添加以前進行判重。獲取待爬取的URL後判斷管理器中是否還有待爬取的URL。當完成爬取後將該URL移動到已爬取URL集合中。python

    實現方式:1.內存 Python內存中直接存儲在set結構中,考慮到set的結構特性web

                      2.關係數據庫 MySQL中正則表達式

                      3.緩存數據庫 redis也能夠實現功能redis

     2、網頁下載器數據庫

    網頁下載器(HTML_download):將互聯網網頁的URL以HTML的形式下載到本地成爲本地文件或者是字符串。緩存

    常見的網頁下載器:官方庫urllib2和request等第三方package。本次就以urllib2做爲網頁下載器。cookie

    urllib2的使用方法: 工具

                                    常見的例如直接傳入url建立response對象而後進行之後工做的方法比較簡單粗暴。可是實際運用中會遇到 https或是須要登陸才能進行操做等相似有情景要求的網頁就不能直接用簡單的傳入url進行調用urllib2下載了。因而咱們介紹一個比較通用的解決方案。如下是測試代碼:

import urllib.request
from http import cookiejar
 print('通用方法')
cj =cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
urllib.request.install_opener(opener)
response3 = urllib.request.urlopen(url)
print(response3.getcode())
print(response3.read())

     3、網頁解析器

    網頁解析器:從網頁中解析出有價值信息的工具(有價值的數據 新的url列表)

    常見的網頁解析器:1.正則表達式模糊匹配

                                   2.HTML。parser模塊

                                   3.beautifulsoup第三方庫

    後兩種涉及到DOM樹相關知識在HTML中有詳細介紹。

    beautifulsoup: 首先是安裝對應模塊就不在此贅述,要檢查是否成功安裝可使用 pip list 命令在cmd中查看:

                                

能夠看到已經成功安裝了bs4模塊。

    bs4的語法:1.html網頁字符串傳入---->建立beautifulsoup對象---->搜索節點---->訪問節點的相關屬性,包括名稱屬性和文

                           字等。搜索時能夠按照節點的相關屬性進行搜索。

                         2.舉例說明:

<a href=‘123.html’ class=‘article_link’>  Python</a>

                                節點名稱:a

                                節點屬性:href=‘123.html’ class=‘article_link’

                                節點內容:Python

                                 下面是相關的測試代碼:

 from bs4 import BeautifulSoup
import re
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>
"""

soup = BeautifulSoup(html_doc,'html.parser',from_encoding='utf-8')
# 建立一個bs對象 html_doc做爲html文檔字符串 'html.parser'指定bs中的HTML_parser做爲網頁解析器 最後一個參數是文檔編碼方式 ,防止出現亂碼
links = soup.find_all('a')
print ('獲取全部鏈接')
for link in links:
    print (link.name,link['href'],link.get_text)
print ( )
print ('獲取lacie')#嘗試獲取單個指定信息
links_node = soup.find('a',href='http://example.com/lacie')
print (links_node.name,links_node['href'],links_node.get_text)
print ( )
print ('正則匹配')
link_node=soup.find('a',href=re.compile(r"ill"))
print (link_node.name,link_node['href'],link_node.get_text())
print ( )
print ('獲取p段落文字')
p_node=soup.find('p',class_="title")
print (p_node.name,p_node.get_text())
#正則模糊匹配

    

        以上就是相關主要模塊的實現方式和相關講解,接下來就是實戰代碼的編寫。

相關文章
相關標籤/搜索