【问题标题】:Automatic download from sciencedirect从 sciencedirect 自动下载
【发布时间】:2013-10-27 15:50:39
【问题描述】:

我正在尝试自动从科学直接下载文章 例如:

url = 'http://www.sciencedirect.com/science/article/pii/S1053811913010240'

我可以使用浏览器毫无问题地访问文章,但我尝试使用 Python 的 requestsurllib2mechanize 模块但没有成功。由于我需要下载很多文章,因此无法手动下载。

Wget 也不起作用。

例如

wget http://www.sciencedirect.com/science/article/pii/S1053811913010240

返回:

HTTP request sent, awaiting response... 404 Not Found

任何想法可能是什么问题?

【问题讨论】:

    标签: python web-scraping wget python-requests


    【解决方案1】:

    它们可能无法正常工作,因为 Web 服务器不喜欢用户代理。也许它试图阻止批量下载。

    如果您使用wget 指定用户代理,它可以工作。用你的例子。

    wget -U "Mozilla/5.0" "https://www.sciencedirect.com/science/article/pii/S1053811913010240"
    

    【讨论】:

    • 实际上,它没有“工作”,它只是拉下一个没有文章内容的充满 javascript 的 html 页面。 ScienceDirect 可能不赞成人们试图抓取他们的内容,这样做可能违反了 TOS。也就是说,您可能可以使用类似于 python 绑定到 WebKitGtk 来自动加载页面,连接到 load-status 属性以在页面加载时获得通知并检索加载的页面。见webkitgtk.org/reference/webkitgtk/stable/…
    【解决方案2】:

    这里有一些我修改为在 pyscholar 中工作的代码。

    #!/usr/bin/python
    #author: Bryan Bishop <kanzure@gmail.com>
    #date: 2010-03-03
    #purpose: given a link on the command line to sciencedirect.com, download the associated PDF and put it in "sciencedirect.pdf" or something
    import os
    import re
    import pycurl
    #from BeautifulSoup import BeautifulSoup
    from lxml import etree
    import lxml.html
    from StringIO import StringIO
    from string import join, split
    
    user_agent = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.5) Gecko/20091123 Iceweasel/3.5.5 (like Firefox/3.5.5; Debian-3.5.5-1)"
    
    def interscience(url):
        '''downloads the PDF from sciencedirect given a link to an article'''
        url = str(url)
        buffer = StringIO()
    
        curl = pycurl.Curl()
        curl.setopt(curl.URL, url)
        curl.setopt(curl.WRITEFUNCTION, buffer.write)
        curl.setopt(curl.VERBOSE, 0)
        curl.setopt(curl.USERAGENT, user_agent)
        curl.setopt(curl.TIMEOUT, 20)
        curl.perform()
        curl.close()
    
        buffer = buffer.getvalue().strip()
        html = lxml.html.parse(StringIO(buffer))
    
        pdf_href = []
        for item in html.getroot().iter('a'):
            if (('id' in item.attrib) and  ('href' in item.attrib) and item.attrib['id']=='pdfLink'):
                pdf_href.append(item.attrib['href'])
    
    
        pdf_href = pdf_href[0]
        #now let's get the article title
    
        title_div = html.find("head/title")
        paper_title = title_div.text
        paper_title = paper_title.replace("\n", "")
        if paper_title[-1] == " ": paper_title = paper_title[:-1]
        re.sub('[^a-zA-Z0-9_\-.() ]+', '', paper_title)
        paper_title = paper_title.strip()
        paper_title = re.sub(' ','_',paper_title)
    
        #now fetch the document for the user
        command = "wget --user-agent=\"pyscholar/blah\" --output-document=\"%s.pdf\" \"%s\"" % (paper_title, pdf_href)
        os.system(command)
        print "\n\n"
    
    interscience("http://www.sciencedirect.com/science/article/pii/S0163638307000628")
    

    【讨论】:

    • 尽量不要只给出代码,而是解释它,让其他用户不仅可以使用它,而且可以从中学习。
    猜你喜欢
    • 1970-01-01
    • 2022-10-03
    • 2023-03-13
    • 2014-02-14
    • 1970-01-01
    • 2017-01-13
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多