当前位置:首页 > Web开发 > 正文

int(page) + 1 ):new_url = url + str(page1)result = urllib.r

2024-03-31 Web开发

标签:

通过urllib.request爬取CSDN博客原创博客标题要领封装

正则表达式:pat = ‘<span>原创</span>(.*)</a>‘

import re import urllib.request def get_csdn(url, pat, page): title_list = [] for page1 in range(1, int(page) + 1): new_url = url + str(page1) result = urllib.request.urlopen(new_url).read().decode("utf-8") string = re.compile(pat).findall(result) for title in string: j = title_list.append(title.strip()) # str.strip()去空格 return title_list if __name__ == __main__: url = "https://blog.csdn.net/weixin_42760923/article/list/" pat = <span>原创</span>(.*)</a> page = 5 print(get_csdn(url, pat, page)) print(len(get_csdn(url, pat, page)))

返回功效:

技术图片

通过urllib.request爬取CSDN原创博客标题要领封装

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/32414.html