5
$\begingroup$

I am new to Wolfram and i would like to extract a few words from a website based on locating a key string.

Specifically, i am trying to access the website: https://www.sec.gov/Archives/edgar/data/2178/000000217818000009/a2017form10-k.htm and locate the string "Address of Principal Executive Offices" and then scrape the address located right before the string "Address of Principal Executive offices".

I have run the following codes:

data = Import["https://www.sec.gov/Archives/edgar/data/2178/000000217818000009/a2017form10-k.htm", "Plaintext"]

this line above imports the link above.

keystring = StringPosition[data, {"Address of Principal Executive Offices"}]

This code above locates the string position at {{746, 783}}

How do I extract 10 words located right before the initial position of {{746}} so I could scrape the actual address and save that to Excel?

May i also ask that if i have a batch of websites saved on a .csv file, how i would apply "StringCases[plainText, "(I.R.S. Employer Identification No.)\n\n" ~~ a___ ~~ "\n\n(Address of Principal Executive Offices)" :> a];" to all of them?

For now, i have the following codes:

dataset = Import[FileNameJoin[{NotebookDirectory[], "linksto10k11.csv"}],"Dataset", HeaderLines -> 1];
dataWithProxyStatementURL = dataset[All, 13];

Above codes import the csv file i have with links to 10-K websites. Then, i run the following code to hopefully retrieve the actual address as above.

processProxyURL[proxyURL_] :=  Module[{plainText, address},plainText = Import[proxyURL, "Plaintext"];



address = StringCases[plainText,"(I.R.S. Employer Identification No.)\n\n" ~~ a___ ~~  "\n\n(Address of Principal Executive Offices)" :> a];    <|"Address" -> address|>];

Allrangeoutput =dataset[All, <|#,"All Range" -> processProxyURL[#["proxyStatementURL"]]|> &];

However, the above codes do not generate the actual address but only generate blanks - may i ask how i could update my codes accordingly so that i could retrieve the actual address?

Thank you so much!

$\endgroup$

3 Answers 3

7
$\begingroup$

This will use the existing boilersplate on the form to "bracket" the data of interest and extract it:

data = Import[
   "https://www.sec.gov/Archives/edgar/data/2178/000000217818000009/a2017form10-k.htm", "Plaintext"];

StringCases[
  data, 
  "(I.R.S. Employer Identification No.)\n\n" ~~ a___ ~~ 
   "\n\n(Address of Principal Executive Offices)" :> a
]

(* Out: {"17 SOUTH BRIAR HOLLOW LANE, SUITE 100, HOUSTON, TEXAS 77027"} *)

You can then use Export to export it to Excel.

This has the added advantage that you do not need to know how long the address is to extract it.

$\endgroup$
1
  • $\begingroup$ Thank you so much for your commenbt MarcoB!!! I have a follow up question on using setting up a function for situations where i have a batch of links. I have an .csv file with links to websites such as one above. May i ask for your expertise on my current codes? I will revise my question above $\endgroup$
    – Dave
    Commented Jan 14, 2022 at 17:38
5
$\begingroup$

An alternative approach: Import as "Data" (instead of "Plaintext") and use Position + Extract:

dt = Import[
 "https://www.sec.gov/Archives/edgar/data/2178/000000217818000009/a2017form10-k.htm", 
 "Data"];

Extract[dt, 
  Flatten @ Position[dt, "(Address of Principal Executive Offices) (Zip Code)"] - {0, 1}]
"17 SOUTH BRIAR HOLLOW LANE, SUITE 100, HOUSTON, TEXAS 77027"
$\endgroup$
4
$\begingroup$

You may use "StringCases" like:

data = Import[
  "https://www.sec.gov/Archives/edgar/data/2178/000000217818000009/\
a2017form10-k.htm", "Plaintext"]

addr = StringCases[data, 
   StartOfLine ~~ (WhitespaceCharacter | WordCharacter | ",") .. ~~ 
    EndOfLine ~~ WhitespaceCharacter .. ~~ 
    "(Address of Principal Executive Offices)"][[1]]

(* 17 SOUTH BRIAR HOLLOW LANE, SUITE 100, HOUSTON, TEXAS 77027 *)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.