亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

基于python實現(xiàn)垂直爬蟲系統(tǒng)的方法詳解

 更新時間:2022年03月04日 10:41:16   作者:bwt_D  
這篇文章主要為大家詳細介紹了python實現(xiàn)垂直爬蟲系統(tǒng)的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助

html_downloader

from urllib import request
def download(url):
    if url is None:
        return
    response = request.urlopen(url)
    if response.getcode() != 200:
        return None
    return response.read()

html_outeputer

data_list = []
def collect_data(data):
    data_list.append(data)
def output_html():
    fout = open('output.html', 'w')
    fout.write('<html>')
    fout.write('<body>')
    fout.write('<table>')
    for dataitem in data_list:
        fout.write('<tr>')
        fout.write('<td>%s</td>' % dataitem['url'])
        fout.write('<td>%s</td>' % dataitem['title'])
        fout.write('<td>%s</td>' % dataitem['datetime'])
        fout.write('<td>%s</td>' % dataitem['visitcount'])
        fout.write('</tr>')
    fout.write('</table>')
    fout.write('</body>')
    fout.write('</html>')
    fout.close()

html_parser

import re
from bs4 import BeautifulSoup
from urllib.parse import urljoin
def get_new_urls(page_url, soup):
    new_urls = set()
    links = soup.find_all('a', href=re.compile(r"/\d+/\d+/\w+/page\.htm"))
    for link in links:
        new_url = link['href']
        new_full_url = urljoin(page_url, new_url)
        new_urls.add(new_full_url)
    return new_urls
def get_new_data(page_url, soup):
    res_data = {}
    title_node = soup.find('h1', class_='arti-title')
    if title_node is None:
        return res_data
    res_data['title'] = title_node.get_text()
    datetime_node = soup.find('span', class_='arti-update')
    res_data['datetime'] = datetime_node.get_text()
    visitcount_node = soup.find('span', class_='WP_VisitCount')
    res_data['visitcount'] = visitcount_node.get_text()
    res_data['url'] = page_url
    return res_data
def parse(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 = get_new_urls(page_url, soup)
    new_data = get_new_data(page_url, soup)
    return new_urls, new_data

spider_main

import urls_manager, html_downloader, \
    html_parser, html_outputer
def craw(root_url):
    count = 1
    urls_manager.add_new_url(root_url)
    #啟動爬蟲循環(huán)
    while urls_manager.has_new_url():
        new_url = urls_manager.get_new_url()
        print('craw %d : %s' % (count, new_url))
        html_cont = html_downloader.download(new_url)
        new_urls, new_data = html_parser.parse(new_url, html_cont)
        urls_manager.add_new_urls(new_urls)
        if new_data:
            html_outputer.collect_data(new_data)
        if count == 10:
            break
        count = count + 1
    html_outputer.output_html()

if __name__ == '__main__':
    root_url = 'http://news.zzuli.edu.cn/'
    craw(root_url)
import urls_manager, html_downloader, \
    html_parser, html_outputer
def craw(root_url):
    count = 1
    urls_manager.add_new_url(root_url)
    #啟動爬蟲循環(huán)
    while urls_manager.has_new_url():
        new_url = urls_manager.get_new_url()
        print('craw %d : %s' % (count, new_url))
        html_cont = html_downloader.download(new_url)
        new_urls, new_data = html_parser.parse(new_url, html_cont)
        urls_manager.add_new_urls(new_urls)
        if new_data:
            html_outputer.collect_data(new_data)
        if count == 10:
            break
        count = count + 1
    html_outputer.output_html()

if __name__ == '__main__':
    root_url = 'http://news.zzuli.edu.cn/'
    craw(root_url)

test_64

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  class="sister" id="link1">Elsie</a>,
<a  class="sister" id="link2">Lacie</a> and
<a  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')
print('獲取所有鏈接')
links = soup.find_all('a')
for link in links:
    print(link.name, link['href'], link.get_text())
print('獲取lacie鏈接')
link_node = soup.find('a', )
print(link_node.name, link_node['href'], link_node.get_text())
print('正則匹配')
link_node = soup.find('a', href=re.compile(r'ill'))
print(link_node.name, link_node['href'], link_node.get_text())
print('獲取P段落文字')
p_node = soup.find('p', class_='title')
print(p_node.name, p_node.get_text())

urls_manager

new_urls = set()
old_urls = set()
def add_new_url(url):
    if url is None:
        return
    if url not in new_urls and url not in old_urls:
        new_urls.add(url)
def add_new_urls(urls):
    if urls is None or len(urls) == 0:
        return
    for url in urls:
        add_new_url(url)
def get_new_url():
    new_url = new_urls.pop()
    old_urls.add(new_url)
    return new_url
def has_new_url():
    return len(new_urls) != 0

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容! 

相關(guān)文章

  • matplotlib實現(xiàn)矩陣和圖像的可視化表示

    matplotlib實現(xiàn)矩陣和圖像的可視化表示

    這篇文章主要為大家詳細介紹了如何利用matplotlib實現(xiàn)矩陣和圖像的可視化表示,文中的示例代碼講解詳細,具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以了解下
    2024-03-03
  • 如何使用 Pylint 來規(guī)范 Python 代碼風(fēng)格(來自IBM)

    如何使用 Pylint 來規(guī)范 Python 代碼風(fēng)格(來自IBM)

    本文通過詳細的理論介紹和簡單易懂的實例全面介紹了 Python 代碼分析工具 Pylint。相信讀者看完后一定可以輕松地將 Pylint 運用到自己的開發(fā)工程中
    2018-04-04
  • pytorch 中forward 的用法與解釋說明

    pytorch 中forward 的用法與解釋說明

    這篇文章主要介紹了pytorch 中forward 的用法與解釋說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • python關(guān)閉占用端口方式

    python關(guān)閉占用端口方式

    今天小編就為大家分享一篇python關(guān)閉占用端口方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • Python實現(xiàn)甘特圖繪制的示例詳解

    Python實現(xiàn)甘特圖繪制的示例詳解

    相信在平常實際工作當(dāng)中,需要對整體的項目做一個梳理,這時如果有一個網(wǎng)頁應(yīng)用能夠?qū)φw項目有一個可視化頁面的展示,是不是會對你的實際工作有所幫助呢?今天小編就通過Python+Streamlit框架來繪制甘特圖并制作可視化大屏,需要的可以參考一下
    2023-04-04
  • Python能做什么

    Python能做什么

    在本篇文章里小編給大家整理的是關(guān)于Python作用領(lǐng)域等相關(guān)知識點,需要的朋友們可以參考下。
    2020-06-06
  • web.py中調(diào)用文件夾內(nèi)模板的方法

    web.py中調(diào)用文件夾內(nèi)模板的方法

    這篇文章主要介紹了web.py中調(diào)用文件夾內(nèi)模板的方法,竟然如此的簡單,而且好用,需要的朋友可以參考下
    2014-08-08
  • Python使用windows設(shè)置定時執(zhí)行腳本

    Python使用windows設(shè)置定時執(zhí)行腳本

    這篇文章主要介紹了Python使用windows設(shè)置定時執(zhí)行腳本,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-11-11
  • python中字符串比較使用is、==和cmp()總結(jié)

    python中字符串比較使用is、==和cmp()總結(jié)

    在Python中比較字符串最好是使用簡單邏輯操作符,今天為大家講解一下is、==和cmp()使用總結(jié)
    2018-03-03
  • python使用opencv按一定間隔截取視頻幀

    python使用opencv按一定間隔截取視頻幀

    這篇文章主要為大家詳細介紹了python使用opencv按一定間隔截取視頻幀,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-03-03

最新評論