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
(初学)requests库爬取bt蚂蚁资源
最新推荐文章于 2022-05-09 00:02:27 发布
本文介绍如何利用requests库爬取BT蚂蚁网站上的资源。首先导入requests和re模块,定义getHTMLtext函数获取网页源代码,然后通过正则表达式解析HTML,提取所需链接。HTMLparse函数用于处理和清洗数据,最后printList函数展示爬取结果。用户输入搜索关键字,程序将爬取相应页面并显示资源列表。
摘要由CSDN通过智能技术生成