如何使用python通过代理进行wget调用?

时间:2011-08-08 10:09:31

标签: python wget google-scholar

我尝试使用此脚本pdfmeat来获取有关Google学者论文的数据。

这个脚本在我的电脑上运行得很好,但是当我尝试将这个脚本放在我的服务器中时,我没有结果。我看到很可能是我的服务器在google学者的黑名单中,给我一个错误(重定向来解决一个chapta):

$ wget scholar.google.com
--2011-08-08 04:52:19--  http://scholar.google.com/
Resolving scholar.google.com... 72.14.204.147, 72.14.204.99, 72.14.204.103, ...
Connecting to scholar.google.com|72.14.204.147|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://www.google.com/sorry/?continue=http://scholar.google.com/ [following]
--2011-08-08 04:52:24--  http://www.google.com/sorry/?continue=http://scholar.google.com/
Resolving www.google.com... 74.125.93.147, 74.125.93.99, 74.125.93.103, ...
Connecting to www.google.com|74.125.93.147|:80... connected.
HTTP request sent, awaiting response... 503 Service Unavailable
2011-08-08 04:52:24 ERROR 503: Service Unavailable.

然后我发现wget中有一个选项--execute“http_proxy = urltoproxy”。我做到了

wget -e "http_proxy=oneHttpProxy" scholar.google.com

我可以从谷歌学者那里保存index.html。

然后我尝试了同样的pdfmeat.py我也没有结果。

这是代码:

def getWebdata(self, link, referer='http://scholar.google.com'):
    useragent = 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.8) Gecko/20100214 Ubuntu/9.10 (karmic) Firefox/3.5.8'
    c_web = 'wget --execute "http_proxy=oneHttpProxy" -qO- --user-agent="%s" --load-cookies="%s" "%s" --referer="%s"' % (useragent, WGET_COOKIEFILE, link, referer) 
    c_out = os.popen(c_web)
    c_txt = c_out.read()
    c_out.close()
    if re.search("We're sorry", c_txt) or re.search("please type the characters", c_txt):
        self.logger.critical("scholar captcha")
        if not self.options.quiet:
            print "PDFMEAT: scholar captcha!"
        sys.exit()
    self.logger.debug("getwebdata excerpt: %s" % (re.sub("\n", " ", c_txt[0:255])))
    self.queryLog.append("getwebdata excerpt: %s" % (re.sub("\n", " ", c_txt[0:255])))
    return c_txt

该脚本使用模块os。原始函数没有wget的--execute选项。

提前致谢

1 个答案:

答案 0 :(得分:0)

您是否尝试过设置http_proxy env。变种?

所以:

$ export http_proxy =“oneHttpProxy”

$ python pdfmeat.py ....