第一次尝试网页抓取和在最后10%的损失我的问题。你知道吗
有什么窍门可以让我丢失的请求与php链接下载吗?你知道吗
我试图从一个会议记录页面(php页面)中提取一些pdf。我可以成功地登录并导航到使用Selenium链接PDF文件的所有单独页面(第一次,记住,所以我喜欢看到页面被导航),并且可以提取所有论文的标题。然后我尝试下面的方法来获取链接的内容。你知道吗
# Get the links
pdfLinks = browser.find_elements_by_tag_name('a')
for pdfItem in range(len(pdfLinks)):
if re.search('loadPDF', pdfLinks[pdfItem].get_attribute('href')):
print(pdfLinks[pdfItem].text)
上面的代码正确地识别了所有的链接文本,到目前为止还不错。你知道吗
# Trying to prove out a simple save
for pdfItem in range(len(pdfLinks)):
if re.search('session-5.5', pdfLinks[pdfItem].get_attribute('href')):
print('Requesting "{0:s}" from {1:s}'.format(pdfLinks[pdfItem].text, str(pdfLinks[pdfItem].get_attribute('href'))))
res = requests.get(pdfLinks[pdfItem].get_attribute('href'), stream=True, headers={'User-Agent': 'firefox'})
res.raise_for_status()
pdfFile = open('{0:s}.pdf'.format(pdfLinks[pdfItem].text), 'wb')
for chunk in res.iter_content(100000):
pdfFile.write(chunk)
pdfFile.close()
我从中得到的响应头是:
{'content-length': '112', 'date': 'Tue, 07 Jul 2015 13:46:27 GMT', 'connection': 'Keep-Alive', 'content-encoding': 'gzip', 'content-type': 'text/html', 'pragma': 'no-cache', 'set-cookie': 'PHPSESSID=7d62686unhha7q0muh4u41pe27; path=/', 'expires': 'Thu, 19 Nov 1981 08:52:00 GMT', 'vary': 'Accept-Encoding', 'keep-alive': 'timeout=5, max=100', 'cache-control': 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0', 'accept-ranges': 'none', 'server': 'Apache'}
我尝试过同样的代码删除res.raise_for_status()
和stream=True, headers={'User-Agent': 'firefox'}
位,但没有成功。你知道吗
如果我实际单击链接并在浏览器控制台中检查GET请求,我会看到请求头和发送的COOKIE信息,后面是响应头:
Response Headers Δ81ms
Server: Apache
Pragma: no-cache
Keep-Alive: timeout=5, max=100
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Date: Tue, 07 Jul 2015 14:23:25 GMT
Content-Type: application/pdf
Content-Transfer-Encoding: binary
Content-Length: 1160565
Content-Disposition: inline; filename="Custom file name for the.pdf"
Connection: Keep-Alive
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Accept-Ranges: bytes
因此,delta似乎是一个cookie与GET一起通过物理单击发送。你知道吗
代码可能很冗长,但我尝试先学习,然后再优化。在这一点上,它会更快地点击每个链接和重命名文件,但我想知道这是如何工作的。你知道吗
谢谢你的指点。你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐