本文介绍了我应该用什么来打开urllib3中的urlopen而不是urlopen的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一段如下代码:

from bs4 import BeautifulSoup
import urllib2

url = 'http://www.thefamouspeople.com/singers.php'
html = urllib2.urlopen(url)
soup = BeautifulSoup(html)

但是我发现我必须立即安装urllib3软件包.

But I found that I have to install urllib3 package now.

此外,我找不到任何教程或示例来了解如何重写以上代码,例如,urllib3没有urlopen.

Moreover, I couldn't find any tutorial or example to understand how to rewrite the above code, for example, urllib3 does not have urlopen.

请解释或举例?!

P/S:我正在使用python 3.4.

P/S: I'm using python 3.4.

推荐答案

urllib3是与urllib和urllib2不同的库.它具有标准库中urllib的许多附加功能,如果需要它们,例如重复使用连接.该文档位于此处: https://urllib3.readthedocs.org/

urllib3 is a different library from urllib and urllib2. It has lots of additional features to the urllibs in the standard library, if you need them, things like re-using connections. The documentation is here: https://urllib3.readthedocs.org/

如果要使用urllib3,则需要pip install urllib3.一个基本示例如下:

If you'd like to use urllib3, you'll need to pip install urllib3. A basic example looks like this:

from bs4 import BeautifulSoup
import urllib3

http = urllib3.PoolManager()

url = 'http://www.thefamouspeople.com/singers.php'
response = http.request('GET', url)
soup = BeautifulSoup(response.data)

这篇关于我应该用什么来打开urllib3中的urlopen而不是urlopen的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-03 09:21