在urllib3中我应该使用什么而不是urlopen

时间:2022-04-24 18:10:42

I wanted to write a code like before... like this:

我想像以前一样编写代码......像这样:

from bs4 import BeautifulSoup
import urllib2

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

But I found I have to install urllib3 now.

但我发现我现在必须安装urllib3。

But coudntfind any tutorial or example to understand how to rewrite above code, for example, urllib3 does not have urlopen.

但是请找出任何教程或示例来了解如何重写上面的代码,例如,urllib3没有urlopen。

Any explanation or example please?!

请问任何解释或示例?!

I'm using python 3.4.

我正在使用python 3.4。

3 个解决方案

#1


21  

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是与urllib和urllib2不同的库。如果您需要它,它还具有标准库中urllib的许多附加功能,例如重用连接。文档在这里:https://urllib3.readthedocs.org/

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

如果你想使用urllib3,你需要pip install urllib3。一个基本的例子如下:

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)

#2


12  

You do not have to install urllib3. You can choose any HTTP-request-making library that fits your needs and feed the response to BeautifulSoup. The choice is though usually requests because of the rich feature set and convenient API. You can install requests by entering pip install requests in the command line. Here is a basic example:

您不必安装urllib3。您可以选择任何符合您需求的HTTP请求制作库,并将响应提供给BeautifulSoup。尽管通常是请求,但由于丰富的功能集和方便的API。您可以通过在命令行中输入pip安装请求来安装请求。这是一个基本的例子:

from bs4 import BeautifulSoup
import requests

url = "url"
response = requests.get(url)

soup = BeautifulSoup(response.content, "html.parser")

#3


2  

The new urllib3 library has a nice documentation here
In order to get your desired result you shuld follow that:

新的urllib3库在这里有一个很好的文档为了获得你想要的结果你shuld遵循:

Import urllib3
from bs4 import BeautifulSoup

url = 'http://www.thefamouspeople.com/singers.php'

http = urllib3.PoolManager()
response = http.request('GET', url)
soup = BeautifulSoup(response.data.decode('utf-8'))

The "decode utf-8" part is optional. It worked without it when i tried, but i posted the option anyway.
Source: User Guide

“decode utf-8”部分是可选的。当我尝试时它没有它,但我仍然发布了选项。来源:用户指南

#1


21  

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是与urllib和urllib2不同的库。如果您需要它,它还具有标准库中urllib的许多附加功能,例如重用连接。文档在这里:https://urllib3.readthedocs.org/

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

如果你想使用urllib3,你需要pip install urllib3。一个基本的例子如下:

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)

#2


12  

You do not have to install urllib3. You can choose any HTTP-request-making library that fits your needs and feed the response to BeautifulSoup. The choice is though usually requests because of the rich feature set and convenient API. You can install requests by entering pip install requests in the command line. Here is a basic example:

您不必安装urllib3。您可以选择任何符合您需求的HTTP请求制作库,并将响应提供给BeautifulSoup。尽管通常是请求,但由于丰富的功能集和方便的API。您可以通过在命令行中输入pip安装请求来安装请求。这是一个基本的例子:

from bs4 import BeautifulSoup
import requests

url = "url"
response = requests.get(url)

soup = BeautifulSoup(response.content, "html.parser")

#3


2  

The new urllib3 library has a nice documentation here
In order to get your desired result you shuld follow that:

新的urllib3库在这里有一个很好的文档为了获得你想要的结果你shuld遵循:

Import urllib3
from bs4 import BeautifulSoup

url = 'http://www.thefamouspeople.com/singers.php'

http = urllib3.PoolManager()
response = http.request('GET', url)
soup = BeautifulSoup(response.data.decode('utf-8'))

The "decode utf-8" part is optional. It worked without it when i tried, but i posted the option anyway.
Source: User Guide

“decode utf-8”部分是可选的。当我尝试时它没有它,但我仍然发布了选项。来源:用户指南