日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python爬取百度百科表格_第一个python爬虫(python3爬取百度百科1000个页面)

發布時間:2025/3/20 python 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python爬取百度百科表格_第一个python爬虫(python3爬取百度百科1000个页面) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

以下內容參考自:http://www.imooc.com/learn/563

一、爬蟲簡介

爬蟲:一段自動抓取互聯網信息的程序

爬蟲可以從一個url出發,訪問其所關聯的所有的url。并從每個url指向的網頁中,獲取我們所需要的信息。

二、簡單爬蟲架構

1.Python簡單爬蟲架構

(1)爬蟲調度端:啟動爬蟲、停止爬蟲、監視爬蟲的運行情況。

(2)在爬蟲程序中,有三個模塊:

1)Url管理器:管理將要爬取的url和已經爬取的url。將待爬取的url傳送給網頁下載器。 2)網頁下載器:將Url指定的網頁下載下來,保存為一個字符串。將這個字符串傳送給網頁解析器進行解析。 3)網頁解析器:一方面,會解釋出有價值的數據;另一方面,解析出字符串中的url,將其補充到url管理器。 這三個模塊,形成了一個循環。只有有未爬取的url,這個循環就會一直繼續下去。

2.Python簡單爬蟲架構的動態運行流程

三、分別講解各模塊設計思路

1.URL管理器

作用:管理待抓取URL集合和已抓取URL集合

目的:防止重復抓取、防止循環抓取

實現方式:

在這里,我們選用Python的set()來實現小型的url管理器

2.網頁下載器

作用:將互聯網上URL對應的網頁下載到本地的工具

工作流程:

實現方式:

3.網頁解析器作用:從網頁中提取有價值的數據從下載好的html網頁或者字符串中,可以提取出url、有價值的數據。類型:正則表達式、html.parser、BeautifulSoup、lxml其中,BeautifulSoup這個第三方插件可以使用html.parser和lxml作為它的解析器。其中,正則表達式是模糊匹配,另外三種則是結構化的解析。附,結構化解析:

四、實戰:使用Python爬蟲抓取百度百科的1000個頁面1.分析目標目標:百度百科Python詞條相關詞條網頁-標題和簡介入口頁:http://baike.baidu.com/view/21087.htmURL格式:--詞條頁面URL:/view/21087.htm數據格式:--標題:

***

--簡介:***頁面編碼:UTF-82.調度程序程序目錄:

spider_main.py

from baike_spider import html_downloader

from baike_spider import html_parser

from baike_spider import 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, root_url):

count = 1

self.urls.add_new_url(root_url)

while self.urls.has_new_url():

try:

new_url = self.urls.get_new_url()

print("craw %d : %s" %(count, new_url))

html_cont = self.downloader.download(new_url)

new_urls, new_data = self.parser.paser(new_url, html_cont)

self.urls.add_new_urls(new_urls)

self.outputer.collect_data(new_data)

if count == 1000:

break

count = count + 1

except:

print('craw failed')

self.outputer.output_html()

if __name__ == '__main__':

root_url = "https://baike.baidu.com/item/Python"

obj_spider = SpiderMain()

obj_spider.craw(root_url)html_download.py

import urllib.request

class HtmlDownloader(object):

def download(self, url):

if url is None:

return None

response = urllib.request.urlopen(url)

if response.getcode() != 200:

return None

return response.read()

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.add_new_url(url)

def has_new_url(self):

return len(self.new_urls) != 0

def get_new_url(self):

new_url = self.new_urls.pop()

self.old_urls.add(new_url)

return new_url

from bs4 import BeautifulSoup

import re

import urllib.parse

class HtmlParser(object):

def _get_new_urls(self, page_url, soup):

new_urls = set()

links = soup.find_all('a', href=re.compile(r'/item/'))

for link in links:

new_url = link['href']

new_full_url = urllib.parse.urljoin(page_url, new_url)

new_urls.add(new_full_url)

return new_urls

def _get_new_data(self, page_url, soup):

res_data = {}

# url

res_data['url'] = page_url

title_node = soup.find('dd', class_="lemmaWgt-lemmaTitle-title").find('h1')

res_data['title'] = title_node.get_text()

# lemma-summary

summary_node = soup.find('div', class_="lemma-summary")

res_data['summary'] = summary_node.get_text()

return res_data

def paser(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

class HtmlOutputer(object):

def __init__(self):

self.datas = []

def collect_data(self, data):

if data is None:

return

self.datas.append(data)

def output_html(self):

fout = open('output.html', 'w', encoding="utf-8")

fout.write("")

fout.write("

")

fout.write("

")

fout.write("

for data in self.datas:

fout.write("

")

fout.write("

%s" % data['url'])

fout.write("

%s" % data['title'])

fout.write("

%s" % data['summary'])

fout.write("

")

fout.write("

")

fout.write("")

fout.write("")

fout.close()

運行結果如下

還會生成一個html記錄文件

項目github地址:https://github.com/oldbig-carry/python_baidu_spider

總結

以上是生活随笔為你收集整理的python爬取百度百科表格_第一个python爬虫(python3爬取百度百科1000个页面)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。