【问题标题】:Recover disk space due to web scraping in Python由于 Python 中的网络抓取而恢复磁盘空间
【发布时间】:2013-11-25 13:19:15
【问题描述】:

我正在使用以下 Python 代码来抓取新闻网站以收集新闻文章:

import mechanize
import re
import time
from selenium import webdriver
from bs4 import BeautifulSoup


url = "http://www.thehindu.com/archive/web/2013/07/01/"

link_dictionary = {}
driver = webdriver.Firefox()
driver.get(url)
time.sleep(10)
soup = BeautifulSoup(driver.page_source)

for tag_li in soup.findAll('li', attrs={"data-section":"Op-Ed"}):
    for link in tag_li.findAll('a'):
        link_dictionary[link.string] = link.get('href')
        urlnew = link_dictionary[link.string]
        brnew =  mechanize.Browser()
        htmltextnew = brnew.open(urlnew).read()            
        articletext = ""
        soupnew = BeautifulSoup(htmltextnew)
        for tag in soupnew.findAll('p'):
            articletext += tag.text
        print "opinion " + re.sub('\s+', ' ', articletext, flags=re.M)
driver.close()

上面的代码是针对某一天的。当我运行此代码一两个月时,它消耗了我的 C:\ 驱动器的大约 3GB 内存空间(我正在使用 Windows7)。

我不知道它如何以及为什么会消耗这么多内存。有人可以向我解释这种现象并帮助恢复丢失的记忆吗?我是 Python 编程新手。

【问题讨论】:

  • driver.quit()P.S.如果你想找回你的记忆,找到硒(可能)的位置,希望缓存会在那里的某个地方
  • @casanova:如果我能要求您接受这里有经验的社区成员的建议,我将不胜感激。我编辑了您的问题,因为(a)多个问号通常不是很好的语法,并且(b)我们倾向于不鼓励称呼,提前感谢和签名。考虑将您的问题写成 Wikipedia 文章。如果您不再恢复我的编辑,我将不胜感激(但欢迎您进一步改进)。提前感谢您 - 很高兴您能得到所需的答案。
  • @Vik2015 我在哪里可以找到selenium .. 我找不到它?你能说得更具体些吗?
  • @casanova,我认为它应该在您的 site-packages 文件夹中的某个位置

标签: python python-2.7 selenium web-scraping web-crawler


【解决方案1】:

link_dictionary = {} 将继续增长。

您从不阅读此内容,并且似乎不需要它。

试试这个:

import mechanize
import re
import time
from selenium import webdriver
from bs4 import BeautifulSoup


url = "http://www.thehindu.com/archive/web/2013/07/01/"

driver = webdriver.Firefox()
driver.get(url)
time.sleep(10)
soup = BeautifulSoup(driver.page_source)

for tag_li in soup.findAll('li', attrs={"data-section":"Op-Ed"}):
    for link in tag_li.findAll('a'): 
        urlnew = link.get('href')
        brnew =  mechanize.Browser()
        htmltextnew = brnew.open(urlnew).read()            
        articletext = ""
        soupnew = BeautifulSoup(htmltextnew)
        for tag in soupnew.findAll('p'):
            articletext += tag.text
        print "opinion " + re.sub('\s+', ' ', articletext, flags=re.M)
driver.close()

【讨论】:

  • 先生,我想它会起作用的。但是你能不能给我一个方法来恢复已经丢失的记忆?我已经丢失了大约 10-12GB 的内存。
  • 对不起,我错过了理解。其中一个库必须在某个地方有缓存,删除它应该可以释放您的磁盘空间。记忆是正确的,但如果您在标题中编辑到磁盘空间,您可能会得到更好的响应。
  • 你使用的是linux还是windows?如果 windows 查看 C:\Documents and settings[user]\Application settings\Temp 中是否有文件,如果 linux 查看 /tmp
  • 我使用的是 Windows 7 .. 让我检查一下
  • 先生,我无法打开Documents and settings,因为正在显示access is denied 提示。
【解决方案2】:

你做了一些disk cleanup。这样你应该能够恢复大约 3-4GB 的东西。要获得更多恢复,您可能需要删除一些应用程序数据来恢复更多磁盘空间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    • 2014-04-13
    • 2021-12-31
    • 1970-01-01
    • 2012-06-14
    • 1970-01-01
    • 2019-12-12
    相关资源
    最近更新 更多