Filter löschen
Filter löschen

How to read a website and download pdf files

25 Ansichten (letzte 30 Tage)
Joy Shen
Joy Shen am 3 Apr. 2023
Beantwortet: Voss am 23 Aug. 2023
Is there a way in matlab to essentially batch read a website and download the pdfs without having the specific pdf url?
For example, on the websites I'm looking through, each page has the following type of link where you download the pdf: https://restservice.epri.com/publicdownload/000000000001013457/0/Product
I just have an excel spreadsheet of all the URLs I want to look through, not the PDF file URLs.

Antworten (2)

Saffan
Saffan am 5 Apr. 2023
It can be done using “webread” and “websave” functions.
Here is an example code snippet to extract all the URLs present in a particular webpage:
%extract entire source code of the page
html_text = webread(url);
%extracts URLs present in the source code
all_urls = regexp(html_text,'https?://[^"]+','match');
Once you have obtained the URLs of the downloadable PDFs, you can use the "websave" function to download them. Here is an example code snippet to demonstrate this:
websave('filename.pdf',pdf_url);
  1 Kommentar
Joy Shen
Joy Shen am 1 Mai 2023
Bearbeitet: Joy Shen am 1 Mai 2023
How do I do a batch download though? For example, I have an excel spreadsheet of all the links and the names of the pdf title. I sorted through and stored all the links that match my string. How do I get webread and websave to open each of the links in my excel spreadsheet (which is a 64x1 table) and download the pdf? Especially when the link to the pdf doesn't seem to lead to a .pdf, it leads to a link like this: https://restservice.epri.com/publicdownload/000000000001013457/0/Product
URL = readtable('EPRI NMAC Repository.xlsx','Range','I2:K638'); % Load excel from specified sheet
substr = 'Nuclear Maintenance Applications Center';
name = table2array(URL(:,1));
selectedcol = contains(name,substr);
links = URL(:,3);
selectedlinks= links(selectedcol,:)
%extract entire source code of the page
html_text = webread(selectedlinks);

Melden Sie sich an, um zu kommentieren.


Voss
Voss am 23 Aug. 2023
URL = readtable('EPRI NMAC Repository.xlsx','Range','I2:K638'); % Load excel from specified sheet
substr = 'Nuclear Maintenance Applications Center';
selectedrow = contains(URL{:,1},substr);
pdf_url = URL{selectedrow,3};
% make valid file names from the urls:
pdf_fn = regexprep(pdf_url,'[/\\.*<>|?"]','_');
pdf_fn = strcat(pdf_fn,'.pdf');
% download the pdf files:
for ii = 1:numel(pdf_url)
websave(pdf_fn{ii},pdf_url{ii});
end

Kategorien

Mehr zu Downloads finden Sie in Help Center und File Exchange

Produkte


Version

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by