Entering edit mode
13 months ago
Confused_human
▴
30
Hello everyone,
I have done a mesh search in pubmed for some certain keywords, I have got PMIDs for the result, Now I want to download all pdfs using those PMIDs in R.
I have used this R script
# Install and load necessary packages
install.packages(c("rentrez", "rcrossref", "xml2"))
library(rentrez)
library(rcrossref)
library(xml2)
# Function to get DOI from PubMed ID
get_doi <- function(pmid) {
query <- entrez_fetch(db = "pubmed", id = pmid, rettype = "medline", parsed = TRUE)
doi <- xpathSApply(query, "//PubmedArticleSet/PubmedArticle/PubmedData/ArticleIdList/ArticleId[@IdType='doi']", xmlValue)
if (length(doi) > 0) {
return(doi)
} else {
return(NULL)
}
}
# Function to download PDF to a local folder
download_pdf <- function(doi, folder) {
pdf_link <- cr_works(doi = doi)$data$links[
cr_works(doi = doi)$data$content.type == "application/pdf"
]
if (!is.null(pdf_link)) {
filename <- paste0(folder, "/", doi_to_filename(doi, ".pdf"))
download.file(pdf_link$url, destfile = filename, mode = "wb")
}
}
# Main function to download PDFs
download_pdfs <- function(pmids, folder = "pdf_folder") {
# Create folder if it doesn't exist
if (!file.exists(folder)) {
dir.create(folder)
}
for (pmid in pmids) {
doi <- get_doi(pmid)
if (!is.null(doi)) {
download_pdf(doi, folder)
}
}
}
# Download PDFs for the given PubMed IDs
download_pdfs(pmids)
But I am getting empty folders and this error:
download_pdfs(pmids)
Error in entrez_fetch(db = "pubmed", id = pmid, rettype = "medline", parsed = TRUE) :
At present, entrez_fetch can only parse XML records, got medline
Please help me with this if it can be done using some other way ?
Thank you
I have used this script
and it has downloaded 14 pdfs and for remaining I got this error :
how to resolve this issue ?
The message is clear:
Those PMID numbers are not PMC records, so they don't have PMC IDs. That's probably why you can't download them.
That is a PMID but there is no free text available directly from NCBI. OP needs to visit the publisher site. Looks like free text is available there.