没有名为“ urllib2”的模块

时间:2019-02-23 14:01:00

标签: python python-3.x

enter image description here

我曾尝试运行代码,但收到错误。实际上,我想从随机的Wikipedia页面中提取标题。然后从标题中删除术语-Wikipedia,并将结果分配给band_title。有人可以帮忙吗?我在Jupyter

上使用Python 3

1 个答案:

答案 0 :(得分:-1)

在Python 3中,urllib2模块已拆分为多个名为urllib.request和urllib.error的模块。

ref: urllib2 — extensible library for opening URLs

from urllib import request
from bs4 import BeautifulSoup

crawlRequest = request.urlopen('http://en.wikipedia.org/wiki/Special:Random')

raw_html = crawlRequest
soup = BeautifulSoup(raw_html, 'lxml')

########################################
# do something with the soup
# 
# Examples:
#
# find_page_title = soup.find('title')
# 
# print (find_page_title)
# <title>Mongul - Wikipedia</title>
#
# print (find_page_title.get_text())
# Mongul - Wikipedia
#
# clean_title = find_page_title.get_text()
# print (clean_title.replace('- Wikipedia', '').strip())
# Mongul
########################################

Stack Overflow有很多与使用相关的帖子 BeautifulSoup with Python