当我同时运行我的程序(网络爬虫)时,它通过我的系统需要不同数量的内存或内存,我还测试了其他网络爬虫,我的网络爬虫是ram的两倍,所以我的问题是 如何在python中手动管理内存或ram,(如果可能的话)?
这是我的代码: -
from bs4 import BeautifulSoup
import requests
import MySQLdb as sql
import time
import warnings
print("starting")
warnings.filterwarnings('ignore')
db = sql.connect("localhost", "root", "arpit", "website")
cursor = db.cursor()
db.autocommit(True)
print("connected to database")
url = "http://www.example.com"
extension = ".com"
print("scrapping url -",url)
r = requests.head(url)
cursor.execute("insert ignore into urls(urls,status,status_code)
values(%s,'pending',%s)", [url, r.status_code])
cursor.execute("select status from urls where status ='pending' limit 1")
result = str(cursor.fetchone())
while (result != "None"):
cursor.execute("select urls from urls where status ='pending' limit 1")
result = str(cursor.fetchone())
s_url = result[2:-3]
cursor.execute("update urls set status = 'done' where urls= %s ", [s_url])
if "https" in url:
url1 = url[12:]
else:
url1 = url[11:]
zone = 0
while True:
try:
r = requests.get(s_url,timeout=60)
break
except:
if s_url == "":
print("done")
break
elif zone >= 4:
print("this url is not valid -",s_url)
break
else:
print("Oops! may be connection was refused. Try again...",s_url)
time.sleep(0.2)
zone = zone + 1
soup = BeautifulSoup(r.content.lower(), 'lxml')
links = soup.find_all("a")
for x in links:
a = x.get('href')
if a is not None and a != "":
if a != "" and a.find("\n") != -1:
a = a[0:a.find("\n")]
if a != "" and a[-1] == "/":
a = a[0:-1]
if a != "":
common_extension = [',',' ',"#",'"','.mp3',"jpg",'.wav','.wma','.7z','.deb','.pkg','.rar','.rpm','.tar','.zip','.bin','.dmg','.iso','.toast','.vcd','.csv','.dat','.log','.mdb','.sav','.sql','.apk','.bat','.exe','.jar','.py','.wsf','.fon','.ttf','.bmp','.gif','.ico','.jpeg','.png','.part','.ppt','.pptx','.class','.cpp','.java','.swift','.ods','.xlr','.xls','.xlsx','.bak','.cab','.cfg','.cpl','.dll','.dmp','.icns','.ini','.lnk','.msi','.sys','.tmp','.3g2','.3gp','.avi','.flv','.h264','.m4v','.mkv','.mov','.mp4','.mpg','.vob','.wmv','.doc','.pdf','.txt']
for ext in common_extension:
if ext in a:
a = ""
break
if a != "":
if a[0:5] == '/http':
a = a[1:]
if a[0:6] == '//http':
a = a[2:]
if a[0:len(url1) + 12] == "https://www." + url1:
cursor.execute("insert ignore into urls(urls,status,status_code) values(%s,'pending',%s)",
[a, r.status_code])
elif a[0:len(url1) + 11] == "http://www." + url1:
cursor.execute("insert ignore into urls(urls,status,status_code) values(%s,'pending',%s)",
[a, r.status_code])
elif a[0:len(url1) + 8] == "https://" + url1:
cursor.execute("insert ignore into urls(urls,status,status_code) values(%s,'pending',%s)",
[url + (a[(a.find(extension + "/")) + 4:]), r.status_code])
elif a[0:len(url1) + 7] == "http://" + url1:
cursor.execute("insert ignore into urls(urls,status,status_code) values(%s,'pending',%s)",
[url + (a[(a.find(extension + "/")) + 4:]), r.status_code])
elif a[0:2] == "//" and a[0:3] != "///" and "." not in a and "http" not in a and "www." not in a:
cursor.execute("insert ignore into urls(urls,status,status_code) values(%s,'pending',%s)",
[url + a[1:], r.status_code])
elif a[0:1] == "/" and a[0:2] != "//" and "." not in a and "http" not in a and "www." not in a:
cursor.execute("insert ignore into urls(urls,status,status_code) values(%s,'pending',%s)",
[url + a[0:], r.status_code])
elif 'http' not in a and 'www.' not in a and "." not in a and a[0] != "/":
cursor.execute("insert ignore into urls(urls,status,status_code) values(%s,'pending',%s)",
[url + '/' + a, r.status_code])
cursor.execute("alter table urls drop id")
cursor.execute("alter table urls add id int primary key not null
auto_increment first")
print("new id is created")
答案 0 :(得分:3)
您的代码内存效率非常低,因为您正在进行很多切片 - 并且因为字符串是不可变的,所以每个切片都会分配一个新对象。
例如:
if a[0:5] == '/http'
a = a[1:]
分配一个新字符串,将a
从0
复制到5
,将其与'/http'
进行比较,并将其丢弃;此外,如果它测试相等,它会分配一个新字符串,从a
复制1
到它上面,并抛出a
。如果a
很长,或者如果这种情况发生很多,这可能会成为一个很大的问题。
查看memoryview
s - 这是一种在不复制字符串的情况下对字符串进行切片(嗯,bytes
)的方法。
您可以通过许多其他方式优化代码:
不要为每个链接重新定义common_extension
,而是在循环之前定义一次。
使用a[0:5] == '/http'
。
a.startswith('/http')
使用像url1
这样的正则表达式而不是前4 re.match('https?://(www\.)?' + re.escape(url1), a)
次比较。
如果您正在这样做,而不是为每个链接连接'https?://(www\.)?'
和re.escape(url1)
,请在循环之前执行一次,甚至re.compile
那里的正则表达式
答案 1 :(得分:0)
除了创建和删除对象之外,您无法分配和/或释放内存块,因此无法直接在Python中管理内存>。你可以做的是使用工具来了解代码的哪些部分使用了多少内存。有关详细信息,请参阅,例如https://www.pluralsight.com/blog/tutorials/how-to-profile-memory-usage-in-python
在python中,限制内存使用的方法显然不是创建大型对象列表或其他数据结构。使用回调,产量和/或其他编码实践来限制结构在内存中花费的时间。
另外我建议您将代码提交给代码审查SE,我认为他们也可以提供帮助。
答案 2 :(得分:0)
简短的回答是,您无法手动管理内存。但是,您的问题应该是,我如何减少 Python使用的内存量?
首先,要知道程序分配的内存量与使用的内存量之间存在差异。
其次,避免分配你不需要的记忆。每个切片操作都会创建一个新的str
对象。相反,请使用startswith
方法:
if not a:
if a.startswith('/http'):
a = a[1:]
if a.startswith('//http'):
a = a[2:]
if a[.startswith("https://www." + url1):
cursor.execute("insert ignore into urls(urls,status,status_code) values(%s,'pending',%s)",
[a, r.status_code])
等