python3爬虫学习之——百度百科词条内链获取

这是跟着《python网络数据采集》这本书做的一个小练习。书里爬的是维基,我自己写的是百度百科。

爬取百科页面用了BeautifulSoup,注意的是百度百科的文本内容全都放在<div class="para">中。

由于百度百科URL中链接中含有中文字符,而python3中urllib.parse中quote()函数可以处理中文字符,因此创建了一个函数用于处理爬虫起始时的中文字符。

def quote(item):
    if item is None or item.strip() is None:
        return None
    if isinstance(item,bytes):
        item = item.decode('utf-8')
    return urllib.parse.quote(item)

完整代码如下:

import urllib.request
import urllib,re
from bs4 import BeautifulSoup

#处理URL中文部分
def quote(item):
    if item is None or item.strip() is None:
        return None
    if isinstance(item,bytes):
        item = item.decode('utf-8')
    return urllib.parse.quote(item)

#用beautifulsoup获取需要的html页面,tag指的是bs4里的参数tag,key_dict是bs4里过滤tag的dict参数
def gettext(url,tag,key_dict):
    html = urllib.request.urlopen(url)
    if html is None:
        return None
    try:
        bsObj = BeautifulSoup(html.read(),'lxml')
        text = bsObj.findAll(tag,key_dict)
    except Exception as e:
        pass
    return text

#将content和url写入到txt文件中
def write_txt(url,content):
    url = 'https://baike.baidu.com'+url
    #采取追加的方式将数据写入文件
    file_write = open(r'.\baidubaike.txt','a')
    try:
        file_write.write('\n'+url+','+content)
        file_write.flush()
    except Exception as e:
        pass
    finally:
        file_write.close()

#从获得的页面中解析出新的url以及词条名称
def geturl(match_string,url,tag,key_dict):
    listofitem = []
    content = []
    text = gettext(url,tag,key_dict)
    if text is None:
        return None
    for t in text:
        t = str(t)
        result = match_string.match(t)
        if result is not None:
            listofitem.append(result.group(1))
            #替换爬取的内容中存在的html标签
            c = str(result.group(2)).replace('<b>','')
            content.append(c)
            #一边爬取数据一边将数据存储到txt文件中
            write_txt(result.group(1),c)
    if len(listofitem) == 0:
        return None
    return (listofitem,content)

if __name__ == '__main__':
    #get_items用于存储已经爬取过的页面URL
    get_items = []
    url = 'https://baike.baidu.com'
    item = quote('美术')
    item_match = re.compile(r'<div.*?class="para.*?".*?>.*?<a.*?href="(.*?)".*?>(.*?)</a>')
    tag = 'div'
    key_dict = {'class':'para'}
    (items,content) = geturl(item_match,url+'/item/%s'%item,tag,key_dict)
    if(items,content) is not None:
        while(True):
            newurl = url + items[0]
            (i,c) = geturl(item_match,newurl,tag,key_dict)
            if (i,c) is not None:
                items.extend(i)
                content.extend(c)
                get_items.append(newurl)
                items.remove(items[0])
                #解析5张页面
                if len(get_items) == 5:
                    break

代码的主要功能就是从一个初始百度百科页面开始,爬取该页面下所有的百科词条以及词条的链接,并将结果存储到txt文件中。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Python爬虫入门(urllib+Beautifulsoup) 本文包括:1、爬虫简单介绍2、爬虫架构三大模块3...
    廖少少阅读 10,126评论 0 6
  • http header 消息通常被分为4个部分:general header即头部, request header...
    徐薇薇阅读 32,348评论 0 5
  • Python开发简单爬虫(Python2.X版本,Eclipse工具) 一、爬虫介绍 爬虫调度端:启动、停止爬虫,...
    凛0_0阅读 2,240评论 2 10
  • 单位里三八妇女节的时候都会发六张免费电影券。虽然是福利,可每年我都很头疼,如何才能用掉这电影券。 自然,想快速地用...
    用灯点亮夜的黑阅读 287评论 0 1
  • 你身处由人性驱动的世界,你无法不考验人性,或者无法不被考验人性。 举个例子,假如有人嘴痒,悄悄给你说看见你老公和x...
    CC小连阅读 462评论 1 1

友情链接更多精彩内容