我正在使用 Julia,现在我正在尝试使用 PyCall 包,以便我可以使用 BeautifulSoup 模块进行 Web 解析。我的 Julia 代码看起来像
using PyCall
pyinitialize("python3")
@pyimport bs4 #need BeautifulSoup
@pyimport urllib.request as urllib #need urlopen
url_base = "blah"
html = urllib.urlopen(url_base).read()
soup = bs4.BeautifulSoup(html, "lxml")
但是,当我尝试运行它时,我收到了有关 read()
函数的提示。我首先认为 read()
是一个内置的 Python 函数,但是 pybuiltin("read")
不起作用。
我不确定可以导入什么Python模块来获取读取功能。我尝试导入 io 模块并使用 io.read() ,但这不起作用。此外,使用 Julia 的内置读取函数不起作用,因为 urllib.urlopen(url_base) 是一个 PyObject。
最佳答案
您有一个拼写错误:
html = urllib.urlopen(url_base).read()
应该是
html = urllib.urlopen(url_base)[:read]()
请参阅PyCall文档:
Important: The biggest difference from Python is that object attributes/members are accessed with
o[:attribute]
rather thano.attribute
, so thato.method(...)
in Python is replaced byo[:method](...)
in Julia. Also, you useget(o, key)
rather thano[key]
. (However, you can access integer indices viao[i]
as in Python, albeit with 1-based Julian indices rather than 0-based Python indices.)
关于python - 如何使用 PyCall 在 Julia 中使用内置的 Python read() 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30876779/