gpt4 book ai didi

python - 从变量 Python (urllib2.urlopen) + Beautifulsoup4 打开链接

转载 作者:行者123 更新时间:2023-12-01 04:44:52 24 4
gpt4 key购买 nike

我正在使用 Python 2.7 + urllib2 + Beautifulsoup4

当我有字符串时:

soup = BeautifulSoup(urllib2.urlopen('http://www.some-website.com', 'html'))

它工作得很好,但是当我将 URl 移动到变量时,它不起作用。

variable = 'http://www.some-website.com'
soup = BeautifulSoup(urllib2.urlopen(variable, 'html'))

错误:

edit: errcode is: File "C:\Python27\lib\urllib2.py", line 285, in get_type
raise ValueError, "unknown url type: %s" % self.__original
ValueError: unknown url type: api/Abc-Abc/def/7/179 –
<小时/>

已解决

问题是其中一个链接只是对服务器数据库的引用。

最佳答案

# Note: Make sure you add live website like http://vaibhavmule.com not http://some-website.com
variable = 'http://www.some-website.com' # Do not forget 'http' prefix here

# As you used 'html' which is not parser library.
soup = BeautifulSoup(urllib2.urlopen(variable))

这应该有效。

Reference用于使用解析器库。

关于python - 从变量 Python (urllib2.urlopen) + Beautifulsoup4 打开链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29727538/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com