(初学)requests库爬取bt蚂蚁资源

本文介绍如何利用requests库爬取BT蚂蚁网站上的资源。首先导入requests和re模块,定义getHTMLtext函数获取网页源代码,然后通过正则表达式解析HTML,提取所需链接。HTMLparse函数用于处理和清洗数据,最后printList函数展示爬取结果。用户输入搜索关键字,程序将爬取相应页面并显示资源列表。
摘要由CSDN通过智能技术生成
import requests
import re
def getHTMLtext(url):
	try:
		r = requests.get(url, timeout=30)
		r.raise_for_status()
		r.encoding = r.apparent_encoding
		return r.text
	except:
		print("cannot scrapy the url")
		return ""

def HTMLparse(list, html):
        try:
                plt = re.findall(r'target=\"_blank\">.*<', html)
                for i in range(len(plt)):
                        plt[i] = re.sub(r'<.*>','',plt[i])
                        plt[i] = re.sub(r'target=\"_blank\">', '', plt[i])
                        plt[i] = re.sub(' ' , '', plt[i])
                        plt[i] = re.sub('<', '', plt[i])
                        list.append(plt[i])
        except:
                print("error")     

def printList(list):
	count = 0
	tplt = "{:4}\t{:16}"
	for i in lis
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值