python溴事百科爬虫

import urllib.request
import re
# qianxiao996精心制作
#博客地址:https://blog.csdn.net/qq_36374896

def jokeCrawler(url):
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36"

    }
    req = urllib.request.Request(url,headers=headers)
    response = urllib.request.urlopen(req)

    HTML = response.read().decode("utf-8")

    pat = r'<div class="author clearfix">(.*?)<span class="stats-vote"><i class="number">'

    re_joke = re.compile(pat,re.S)
    divsList = re_joke.findall(HTML)
    # print(divsList)
    # print(len(divsList))
    dic = {}
    for div in divsList:
        re_u = re.compile(r"<h2>(.*?)</h2>",re.S)
        username = re_u.findall(div)
        username = username[0]

        re_d = re.compile(r'<div class="content">\n<span>(.*?)</span>', re.S)
        duzi = re_d.findall(div)
        duzi = duzi[0]
        print(duzi)

        dic[username]=duzi
    return dic

    # with open(r"C:\fle\file1.html","wb") as f:
    #     f.write(HTML)


url= "https://www.qiushibaike.com/text/page/2/"
info = jokeCrawler(url)

for k,v in info.items():
    print(k,v)

代码年代久远,注释当时没写,现在懒得写了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值