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 list:
print(tplt.format(count, i))
count = count + 1
def main():
source = input("请输入要查找的资源:")
page = 1
list = []
for i in range(page):
try:
url = "http://www
(初学)requests库爬取bt蚂蚁资源
最新推荐文章于 2024-07-07 16:39:11 发布
本文介绍如何利用requests库爬取BT蚂蚁网站上的资源。首先导入requests和re模块,定义getHTMLtext函数获取网页源代码,然后通过正则表达式解析HTML,提取所需链接。HTMLparse函数用于处理和清洗数据,最后printList函数展示爬取结果。用户输入搜索关键字,程序将爬取相应页面并显示资源列表。

7808

被折叠的 条评论
为什么被折叠?



