如何从shtml链接集合中获取或下载pdf?

2024-04-23 07:35:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我抓取了一个shtml链接列表。它们现在保存在.xlsx文件中。你知道吗

List

我已经试过寻找excel宏、r代码、python代码、chrome扩展和桌面程序。我找不到任何对我有帮助的研究。你知道吗

每个.shtml链接都指向一个网页,其中至少有一个.pdf位于我需要下载的页面的中心。你知道吗

感谢您的帮助!你知道吗


Tags: 文件代码程序网页列表pdf链接页面
2条回答

这很有帮助!你知道吗

install.packages("rvest")
install.packages("httr")
install.packages("readxl")
update.packages("tibble")

library(rvest)
library(httr)
library(readxl)

setwd("C:/Users/Andreas/Desktop/481064 A.F. - Master Thesis - Election Outcome Prediction/Full Repository Austrian Bundestag")
my_data <- read_excel("StenographischeProto.xlsx")
View(my_data)

session <- html_session("https://www.uscis.gov/sites/default/files/files/form/i-765.pdf")

# save pdf to test.pdf
writeBin(session$response$content,"test.pdf")

基本工作流程是:

  1. 您需要使用cssxpath找到pdf下载按钮。你知道吗
  2. 使用Rselenium to simulate the download action;或者获取href属性并使用rvest向该链接发出请求,然后使用writeBin()将二进制响应写入磁盘

要下载pdf文件,我将以政府表格为例:

pdf网址:https://www.uscis.gov/sites/default/files/files/form/i-765.pdf

library(rvest)
library(httr)

session <- html_session("https://www.uscis.gov/sites/default/files/files/form/i-765.pdf")

# save pdf to test.pdf
writeBin(session$response$content,"test.pdf")

相关问题 更多 >