我在访问通过谷歌硬盘存储的网站上的.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"之间的所有东西都是我想要的超链接。
我尝试过使用requests.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 one和this one )的答案,我可以看出我遗漏了一个步骤,或者可能是整个方法都失败了,但我不太确定从哪里开始。
google drive是任何访问该网站的人都可以访问的,所以我不知道身份验证信息是什么,也没有提到“驱动程序”。
简单地将链接从源代码复制并粘贴到我的浏览器中会返回一个404错误,所以我想我的方法已经走得很远了。
任何人和所有人的帮助都将受到热烈的感谢。
发布于 2020-07-02 18:26:40
问题:
您正在将从网站检索的所有内容设置为小写。驱动器链接基于相应的file id's,该are区分大小写,因此您尝试访问的链接不是有效链接。因此,你可以得到404。
解决方案:
向site发出get请求时,不要将响应设置为小写。更改此设置:
requests.get(site).text.lower()要这样做:
requests.get(site).texthttps://stackoverflow.com/questions/62692504
复制相似问题