从Python中的HTML链接下载文本数据

时间:2018-06-23 13:12:00

标签: python pandas url https

嗨,我想下载HTML链接上托管的带分隔符的文本。 (该链接只能在专用网络上访问,因此不能在此处共享)。

在R中,以下功能解决了这个问题(所有其他功能均出现“未经授权的访问”或“ 401”错误)

<forward name="unspecified" path="/candyStore.jsp"/>

我想在Python中做同样的事情,

(A)urllib和request.get()

url = 'https://dw-results.ansms.com/dw-platform/servlet/results? job_id=13802737&encoding=UTF8&mimeType=plain'
download.file(url, "~/insights_dashboard/testing_file.tsv")
a = read.csv("~/insights_dashboard/testing_file.tsv",header = T,stringsAsFactors = F,sep='\t')

(B)requests.get()和read.html

import urllib.request

url_get = requests.get(url, verify=False)
urllib.request.urlretrieve(url_get, 'C:\\Users\\cssaxena\\Desktop\\24.tsv')

(C)使用wget:

url='https://dw-results.ansms.com/dw-platform/servlet/results? job_id=13802737&encoding=UTF8&mimeType=plain'
s = requests.get(url, verify=False)
a = pd.read_html(io.StringIO(s.decode('utf-8')))

OR

import wget
url = 'https://dw-results.ansms.com/dw-platform/servlet/results? job_id=13802737&encoding=UTF8&mimeType=plain'  
wget.download(url,--auth-no-challenge, 'C:\\Users\\cssaxena\\Desktop\\24.tsv')

注意:URL不需要任何凭据,浏览器可以访问该URL,并且可以使用R和download.file进行下载。我正在寻找Python中的解决方案

1 个答案:

答案 0 :(得分:0)

def geturls(path):
    yy=open(path,'rb').read()
    yy="".join(str(yy))
    yy=yy.split('<a')

    out=[]
    for d in yy:
        z=d.find('href="')
        if z>-1:
            x=d[z+6:len(d)]
            r=x.find('"')
            x=x[:r]
            x=x.strip(' ./')
                 #
            if (len(x)>2) and (x.find(";")==-1):
                out.append(x.strip(" /"))
    out=set(out)
    return(out)

pg="./test.html"# your html

url=geturls(pg)

print(url)