如何防止在 python 2.7 中使用 urllib2 包?

How to prevent the use of urllib2 package in python 2.7?

我正在尝试在下面做同样的事情,但没有使用 urllib2 包。 我正在尝试通过网络抓取 URL(不完全是下面显示的那个)。 出于安全原因,必须 headers。

URL = 'https://www.google.com/search?q=test'
hdr = {'User-Agent': 'Mozilla/5.4'}
req = urllib2.Request(URL, headers=hdr)
pag = urllib2.urlopen(req)
soup = BeautifulSoup(pag, "lxml")
all_tables = soup.find_all('table')
right_table = soup.find_all('table')[1]

我试过在堆栈溢出中查找这个,但我只能使用 urllib2 找到解决方案。 我有理由不想使用 urllib2

有没有不使用 urllib2 的方法? 我正在使用 python 2.7

谢谢。

import requests

URL = 'https://www.google.com/search?q=test'
hdr = {'User-Agent': 'Mozilla/5.4'}
req = requests.get(URL, headers=hdr)
soup = BeautifulSoup(req.content, "lxml")
all_tables = soup.find_all('table')
right_table = soup.find_all('table')[1]