最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

Python 爬蟲介紹

2023-07-23 11:14 作者:bili_46681046406  | 我要投稿

一、什么是爬蟲? ? ? ? ? ?附上學習財料?https://weibo.com/xk23587

爬蟲:一段自動抓取互聯(lián)網(wǎng)信息的程序,從互聯(lián)網(wǎng)上抓取對于我們有價值的信息。

二、Python爬蟲架構(gòu)

Python 爬蟲架構(gòu)主要由五個部分組成,分別是調(diào)度器、URL管理器、網(wǎng)頁下載器、網(wǎng)頁解析器、應(yīng)用程序(爬取的有價值數(shù)據(jù))。

  • 調(diào)度器:相當于一臺電腦的CPU,主要負責調(diào)度URL管理器、下載器、解析器之間的協(xié)調(diào)工作。

  • URL管理器:包括待爬取的URL地址和已爬取的URL地址,防止重復(fù)抓取URL和循環(huán)抓取URL,實現(xiàn)URL管理器主要用三種方式,通過內(nèi)存、數(shù)據(jù)庫、緩存數(shù)據(jù)庫來實現(xiàn)。

  • 網(wǎng)頁下載器:通過傳入一個URL地址來下載網(wǎng)頁,將網(wǎng)頁轉(zhuǎn)換成一個字符串,網(wǎng)頁下載器有urllib2(Python官方基礎(chǔ)模塊)包括需要登錄、代理、和cookie,requests(第三方包)

  • 網(wǎng)頁解析器:將一個網(wǎng)頁字符串進行解析,可以按照我們的要求來提取出我們有用的信息,也可以根據(jù)DOM樹的解析方式來解析。網(wǎng)頁解析器有正則表達式(直觀,將網(wǎng)頁轉(zhuǎn)成字符串通過模糊匹配的方式來提取有價值的信息,當文檔比較復(fù)雜的時候,該方法提取數(shù)據(jù)的時候就會非常的困難)、html.parser(Python自帶的)、beautifulsoup(第三方插件,可以使用Python自帶的html.parser進行解析,也可以使用lxml進行解析,相對于其他幾種來說要強大一些)、lxml(第三方插件,可以解析 xml 和 HTML),html.parser 和 beautifulsoup 以及 lxml 都是以 DOM 樹的方式進行解析的。

  • 應(yīng)用程序:就是從網(wǎng)頁中提取的有用數(shù)據(jù)組成的一個應(yīng)用。

下面用一個圖來解釋一下調(diào)度器是如何協(xié)調(diào)工作的:

三、urllib2 實現(xiàn)下載網(wǎng)頁的三種方式

#!/usr/bin/python# -*- coding: UTF-8 -*- import cookielibimport urllib2 url = "http://www.baidu.com"response1 = urllib2.urlopen(url)print "第一種方法"#獲取狀態(tài)碼,200表示成功print response1.getcode()#獲取網(wǎng)頁內(nèi)容的長度print len(response1.read()) print "第二種方法"request = urllib2.Request(url)#模擬Mozilla瀏覽器進行爬蟲request.add_header("user-agent","Mozilla/5.0")response2 = urllib2.urlopen(request)print response2.getcode()print len(response2.read()) print "第三種方法"cookie = cookielib.CookieJar()#加入urllib2處理cookie的能力opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))urllib2.install_opener(opener)response3 = urllib2.urlopen(url)print response3.getcode()print len(response3.read())print cookie

四、第三方庫 Beautiful Soup 的安裝

Beautiful Soup: Python 的第三方插件用來提取 xml 和 HTML 中的數(shù)據(jù),官網(wǎng)地址https://weibo.com/xk23587

1、安裝 Beautiful Soup

打開 cmd(命令提示符),進入到 Python(Python2.7版本)安裝目錄中的 scripts 下,輸入 dir 查看是否有 pip.exe, 如果用就可以使用 Python 自帶的 pip 命令進行安裝,輸入以下命令進行安裝即可:

pip install beautifulsoup4

2、測試是否安裝成功

編寫一個 Python 文件,輸入:? ?https://weibo.com/xk23587

import bs4print bs4

運行該文件,如果能夠正常輸出則安裝成功。

五、使用 Beautiful Soup 解析 html 文件

#!/usr/bin/python# -*- coding: UTF-8 -*- import re from bs4 import BeautifulSouphtml_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>"""#創(chuàng)建一個BeautifulSoup解析對象soup = BeautifulSoup(html_doc,"html.parser",from_encoding="utf-8")#獲取所有的鏈接links = soup.find_all('a')print "所有的鏈接"for link in links: ? ?print link.name,link['href'],link.get_text() print "獲取特定的URL地址"link_node = soup.find('a',href="http://example.com/elsie")print link_node.name,link_node['href'],link_node['class'],link_node.get_text() print "正則表達式匹配"link_node = soup.find('a',href=re.compile(r"ti"))print link_node.name,link_node['href'],link_node['class'],link_node.get_text() print "獲取P段落的文字"p_node = soup.find('p',class_='story')print p_node.name,p_node['class'],p_node.get_text()


原文地址:https://weibo.com/xk23587


Python 爬蟲介紹的評論 (共 條)

分享到微博請遵守國家法律
中阳县| 定州市| 卢龙县| 读书| 宣化县| 昭通市| 咸丰县| 家居| 八宿县| 曲水县| 木兰县| 房山区| 六枝特区| 噶尔县| 神农架林区| 吴忠市| 新密市| 太保市| 桑植县| 兰考县| 阜南县| 阿荣旗| 天峻县| 常德市| 万州区| 南华县| 舞钢市| 安仁县| 长宁县| 宜黄县| 那坡县| 澄江县| 刚察县| 顺平县| 山丹县| 沧州市| 会理县| 德保县| 铁岭县| 长治县| 华阴市|