python - 如何使用 PyCall 在 Julia 中使用内置的 Python read() 函数?

标签 python io julia

我正在使用 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 than o.attribute, so that o.method(...) in Python is replaced by o[:method](...) in Julia. Also, you use get(o, key) rather than o[key]. (However, you can access integer indices via o[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/

相关文章:

python - 为什么 asyncio 中的异常迟到或根本不出现?

python - 使用 QtWidgets.QSlider 将 Waitkey 更改为 "0"无效

python - 有条件地替换 NaN

python - 有没有办法使用另一个字典在 Python 字典上设置多个默认值?

c# - 如何在通过 FileStream 写入文件时锁定文件?

java - 如何检查文件是否存在于 Java 中?

java - equals()方法未从文件中检测到单词

architecture - Julia:使用许多不同但相关的算法选择来构建代码

string - Julia : How to convert vector of type string to type numeric (Float64)

julia - 将矩阵的行/列提取到单独的变量中