BL_7
BL_7

Reputation: 63

Is it possible to call a Python function from Julia and return its result?

I am using Python to scrape data from the web. I want to use that data to run calculations in Julia.

Is it possible to call the function in Julia and return its result, or am I better off just exporting to a CSV and loading the data in that way?

Upvotes: 3

Views: 471

Answers (2)

Andrej Oskin
Andrej Oskin

Reputation: 2342

Not an answer to your question, but it is rather easy to scrape web in Julia itself

using Gumbo
using Cascadia
using Cascadia: matchFirst
using HTTP

r = HTTP.get("https://stackoverflow.com/questions/46638265")
page = parsehtml(String(r.body))
julia> matchFirst(sel"title", page.root) |> nodeText
"Is it possible to call a Python function from Julia and return its result? - Stack Overflow"

julia> eachmatch(sel".answercell p", page.root) .|> nodeText
2-element Array{String,1}:
 "Absolutely.  See PyCall.jl."
 "Not an answer to your question, but it is rather easy to scrape web in Julia itself"

Upvotes: 2

mbauman
mbauman

Reputation: 31362

Absolutely. See PyCall.jl.

julia> using PyCall

julia> @pyimport bs4

julia> @pyimport requests

julia> r = requests.get("https://stackoverflow.com/questions/46638265");

julia> soup = bs4.BeautifulSoup(r.content);

julia> soup.title.string
"Is it possible to call a Python function from Julia and return its result? - Stack Overflow"

julia> soup.select_one(".answercell p").text
"Absolutely.  See PyCall.jl."

Upvotes: 18

Related Questions