竹内纱里奈和大战黑人_欧美成人黄色小视频_91福利影视_欧美在线观看视频网站_h色网站免费观看_97综合

極客小將

您現(xiàn)在的位置是:首頁 » python編程資訊

資訊內(nèi)容

python網(wǎng)絡(luò)爬蟲步驟是什么

極客小將2021-03-12-
簡介python網(wǎng)絡(luò)爬蟲步驟:首先準(zhǔn)備所需庫,編寫爬蟲調(diào)度程序;然后編寫url管理器,并編寫網(wǎng)頁下載器;接著編寫網(wǎng)頁解析器;最后編寫網(wǎng)頁輸出器即可。本教程操作環(huán)境:windows7系統(tǒng)、python3.9版,DELLG3電腦。python網(wǎng)絡(luò)爬蟲步驟(1)準(zhǔn)備所需庫我們需要準(zhǔn)備一款名為Beautiful

python網(wǎng)絡(luò)爬蟲步驟:首先準(zhǔn)備所需庫,編寫爬蟲調(diào)度程序;然后編寫url管理器,并編寫網(wǎng)頁下載器;接著編寫網(wǎng)頁解析器;**后編寫網(wǎng)頁輸出器即可。KeK少兒編程網(wǎng)-https://www.pxcodes.com

KeK少兒編程網(wǎng)-https://www.pxcodes.com

本教程操作環(huán)境:windows7系統(tǒng)、python3.9版,DELL G3電腦。KeK少兒編程網(wǎng)-https://www.pxcodes.com

python網(wǎng)絡(luò)爬蟲步驟KeK少兒編程網(wǎng)-https://www.pxcodes.com

(1)準(zhǔn)備所需庫KeK少兒編程網(wǎng)-https://www.pxcodes.com

我們需要準(zhǔn)備一款名為BeautifulSoup(網(wǎng)頁解析)的開源庫,用于對下載的網(wǎng)頁進(jìn)行解析,我們是用的是PyCharm編譯環(huán)境所以可以直接下載該開源庫。KeK少兒編程網(wǎng)-https://www.pxcodes.com

步驟如下:KeK少兒編程網(wǎng)-https://www.pxcodes.com

選擇File->SettingsKeK少兒編程網(wǎng)-https://www.pxcodes.com

KeK少兒編程網(wǎng)-https://www.pxcodes.com

打開Project:PythonProject下的Project interpreterKeK少兒編程網(wǎng)-https://www.pxcodes.com

KeK少兒編程網(wǎng)-https://www.pxcodes.com

點(diǎn)擊加號添加新的庫KeK少兒編程網(wǎng)-https://www.pxcodes.com

KeK少兒編程網(wǎng)-https://www.pxcodes.com

輸入bs4選擇bs4點(diǎn)擊Install Packge進(jìn)行下載KeK少兒編程網(wǎng)-https://www.pxcodes.com

KeK少兒編程網(wǎng)-https://www.pxcodes.com

(2)編寫爬蟲調(diào)度程序KeK少兒編程網(wǎng)-https://www.pxcodes.com

這里的bike_spider是項(xiàng)目名稱引入的四個類分別對應(yīng)下面的四段代碼url管理器,url下載器,url解析器,url輸出器。KeK少兒編程網(wǎng)-https://www.pxcodes.com

# 爬蟲調(diào)度程序 from bike_spider import url_manager, html_downloader, html_parser, html_outputer # 爬蟲初始化 class SpiderMain(object): def __init__(self): self.urls = url_manager.UrlManager() self.downloader = html_downloader.HtmlDownloader() self.parser = html_parser.HtmlParser() self.outputer = html_outputer.HtmlOutputer() def craw(self, my_root_url): count = 1 self.urls.add_new_url(my_root_url) while self.urls.has_new_url(): try: new_url = self.urls.get_new_url() print("craw %d : %s" % (count, new_url)) # 下載網(wǎng)頁 html_cont = self.downloader.download(new_url) # 解析網(wǎng)頁 new_urls, new_data = self.parser.parse(new_url, html_cont) self.urls.add_new_urls(new_urls) # 網(wǎng)頁輸出器收集數(shù)據(jù) self.outputer.collect_data(new_data) if count == 10: break count += 1 except: print("craw failed") self.outputer.output_html() if __name__ == "__main__": root_url = "http://baike.baidu.com/item/Python/407313" obj_spider = SpiderMain() obj_spider.craw(root_url)

(3)編寫url管理器KeK少兒編程網(wǎng)-https://www.pxcodes.com

我們把已經(jīng)爬取過的url和未爬取的url分開存放以便我們不會重復(fù)爬取某些已經(jīng)爬取過的網(wǎng)頁。KeK少兒編程網(wǎng)-https://www.pxcodes.com

# url管理器 class UrlManager(object): def __init__(self): self.new_urls = set() self.old_urls = set() def add_new_url(self, url): if url is None: return if url not in self.new_urls and url not in self.old_urls: self.new_urls.add(url) def add_new_urls(self, urls): if urls is None or len(urls) == 0: return for url in urls: self.new_urls.add(url) def get_new_url(self): # pop方法會幫我們獲取一個url并且移除它 new_url = self.new_urls.pop() self.old_urls.add(new_url) return new_url def has_new_url(self): return len(self.new_urls) != 0

(4)編寫網(wǎng)頁下載器KeK少兒編程網(wǎng)-https://www.pxcodes.com

通過網(wǎng)絡(luò)請求來下載頁面KeK少兒編程網(wǎng)-https://www.pxcodes.com

# 網(wǎng)頁下載器 import urllib.request class HtmlDownloader(object): def download(self, url): if url is None: return None response = urllib.request.urlopen(url) # code不為200則請求失敗 if response.getcode() != 200: return None return response.read()

(5)編寫網(wǎng)頁解析器KeK少兒編程網(wǎng)-https://www.pxcodes.com

對網(wǎng)頁進(jìn)行解析時我們需要知道我們要查詢的內(nèi)容都有哪些特征,我們可以打開一個網(wǎng)頁點(diǎn)擊右鍵審查元素來了解我們所查內(nèi)容的共同之處。KeK少兒編程網(wǎng)-https://www.pxcodes.com

# 網(wǎng)頁解析器 import re from bs4 import BeautifulSoup from urllib.parse import urljoin class HtmlParser(object): def parse(self, page_url, html_cont): if page_url is None or html_cont is None: return soup = BeautifulSoup(html_cont, "html.parser", from_encoding="utf-8") new_urls = self._get_new_urls(page_url, soup) new_data = self._get_new_data(page_url, soup) return new_urls, new_data def _get_new_data(self, page_url, soup): res_data = {"url": page_url} # 獲取標(biāo)題 title_node = soup.find("dd", class_="lemmaWgt-lemmaTitle-title").find("h1") res_data["title"] = title_node.get_text() summary_node = soup.find("p", class_="lemma-summary") res_data["summary"] = summary_node.get_text() return res_data def _get_new_urls(self, page_url, soup): new_urls = set() # 查找出所有符合下列條件的url links = soup.find_all("a", href=re.compile(r"/item/")) for link in links: new_url = link['href'] # 獲取到的url不完整,學(xué)要拼接 new_full_url = urljoin(page_url, new_url) new_urls.add(new_full_url) return new_urls

(6)編寫網(wǎng)頁輸出器KeK少兒編程網(wǎng)-https://www.pxcodes.com

輸出的格式有很多種,我們選擇以html的形式輸出,這樣我們可以的到一個html頁面。KeK少兒編程網(wǎng)-https://www.pxcodes.com

# 網(wǎng)頁輸出器 class HtmlOutputer(object): def __init__(self): self.datas = [] def collect_data(self, data): if data is None: return self.datas.append(data) # 我們以html表格形式進(jìn)行輸出 def output_html(self): fout = open("output.html", "w", encoding='utf-8') fout.write("<html>") fout.write("<meta charset='utf-8'>") fout.write("<body>") # 以表格輸出 fout.write("<table>") for data in self.datas: # 一行 fout.write("<tr>") # 每個單元行的內(nèi)容 fout.write("<td>%s</td>" % data["url"]) fout.write("<td>%s</td>" % data["title"]) fout.write("<td>%s</td>" % data["summary"]) fout.write("</tr>") fout.write("</table>") fout.write("</body>") fout.write("</html>") # 輸出完畢后一定要關(guān)閉輸出器 fout.close()

相關(guān)免費(fèi)學(xué)習(xí)推薦:python視頻教程KeK少兒編程網(wǎng)-https://www.pxcodes.com

以上就是python網(wǎng)絡(luò)爬蟲步驟是什么的詳細(xì)內(nèi)容,更多請關(guān)注少兒編程網(wǎng)其它相關(guān)文章!KeK少兒編程網(wǎng)-https://www.pxcodes.com

預(yù)約試聽課

已有385人預(yù)約都是免費(fèi)的,你也試試吧...

主站蜘蛛池模板: 久久久久久久成人 | 国产精品视频一二区 | 欧美不卡 | 久久国产精品精品国产色婷婷 | 黄色小视频在线 | 亚洲乱码精品 | 精品一区二区三区四区五区 | 欧美日韩一区三区 | 国产1区 | 国产激情精品一区二区三区 | 大陆一级毛片免费视频观看 | 精品久久一区 | 在线播放日韩 | 久久久久久久久久影视 | 亚洲精品福利电影 | 黄色片在线免费观看 | 国产激情综合五月久久 | 在线不卡一区 | 91av视频在线免费观看 | 午夜三区| 最新国产精品精品视频 | 久久黄视频 | 欧美激情精品久久久久久久久久 | 久久这里都是精品 | 国产欧美精品一区二区色综合 | 久久99精品久久久久久琪琪 | 久草视频免费 | 国产免费一区二区三区在线能观看 | 91精品一区二区三区蜜桃 | 欧美日韩亚洲自拍 | 欧美日韩中文在线 | 成人夜晚看av | 国产一区二三区 | 羞羞视频在线观看 | 成人在线免费视频 | 精品一区二区三区四区五区 | 色在线免费 | 精品视频免费在线 | 亚洲免费看片 | 欧美一区二区高清 | 国产一区二区免费 |