通过Python请求从Google云端硬盘下载数据

时间:2020-07-02 08:48:57

标签: python python-requests google-drive-api

我无法从网站访问通过google驱动器存储的.pdf数据。

可以在这里找到网站。

源代码显示我所关注的链接很容易识别...

<ul style="margin-left: 40px;">
<li><a href="https://drive.google.com/open?id=11Zw72KDm4cdfswuCjbeM2c3sM6kdcowE" target="_blank">January 4, 6-9, 2020</a></li>
<li><a href="https://drive.google.com/a/agfc.ar.gov/file/d/1OtSZtBxaNWGqlDvBp-cG7TAwOHjYacm_/view?usp=sharing" target="_blank">December 12-20, 2019</a></li>
<li><a href="https://drive.google.com/open?id=1HPa1REOTy_Kz9wxLUpT4N57KEurE8Z9f" target="_blank">November 16-19, 2019</a></li>
<li><a href="https://drive.google.com/open?id=1iCBknPwIxirmWeiD7VPKxwCYvgQUkOB-" target="_blank">January 20-23, 2019</a></li>

...在a href="" target="_blank"之间的所有内容都是我所需要的超链接。

我尝试使用request.get()进行处理。

site = 'site goes here'

url_locs = []

url_locs = BeautifulSoup(requests.get(site).text.lower(), 'html.parser').findAll('ul', {'style': 'margin-left: 40px;'})

# Locate the url for the pdf
report_urls = re.findall('<li><a href="(.*?)" target="', str(url_locs))
#print (report_urls)

# Download and save the individual pdfs, then record the filepath to add to the INDEX
for url in report_urls:
    r = requests.get(url)
    print(r)

...但所有输出均为<Response [404]>

在API中进行一些挖掘,并寻找先前类似问题的答案,例如this onethis one,我可以说我缺少一个步骤,或者可能整个方法都消失了,但我不太确定从这里去哪里。

访问该网站的任何人都可以使用google驱动器,因此我不知道身份验证信息是什么,也没有提及“驱动程序”。

仅将链接从源代码复制并粘贴到我的浏览器中会返回404错误,因此我认为我的方法还差得远。

我们将不胜感激。

1 个答案:

答案 0 :(得分:1)

问题:

您正在将从网站检索的所有内容设置为小写。驱动器链接基于区分大小写的file id's,因此您尝试访问的链接无效。因此,您得到404。

解决方案:

site发出get请求时,请勿将响应设置为小写。更改此:

requests.get(site).text.lower()

对此:

requests.get(site).text