如果网站不存在,如何仍然获得输出。 (如何处理错误404)
How to still get an output if the website doesn't exist. (how to handle Error 404)
代码如下:
quote_page = "https://en.wikipedia.org/wiki/" + Awi
page = urllib2.urlopen(quote_page)
soup = BeautifulSoup(page, 'html.parser')
wiki_box = soup.find('div', attrs={'class':'mw-parser-output'})
wikip = wiki_box.find('p')
wiki = wikip.text.strip()
print wiki
Awi 是 wiki 站点的关键字。但是,如果该站点不存在怎么办。有人可以帮忙吗?
使用 try&except
import urllib2
try:
urllib2.urlopen(quote_page)
except urllib2.HTTPError as e:
print(e.code)
except urllib2.URLError as e:
print(e.args)
您需要获取请求状态码,看是否满足HTTP CODE 200
quote_page = "https://en.wikipedia.org/wiki/" + Awi
page = urllib2.urlopen(quote_page)
if page.getcode() == 200:
soup = BeautifulSoup(page, 'html.parser')
wiki_box = soup.find('div', attrs={'class':'mw-parser-output'})
wikip = wiki_box.find('p')
wiki = wikip.text.strip()
print(wiki)
else:
print("Page could not be loaded")
代码如下:
quote_page = "https://en.wikipedia.org/wiki/" + Awi
page = urllib2.urlopen(quote_page)
soup = BeautifulSoup(page, 'html.parser')
wiki_box = soup.find('div', attrs={'class':'mw-parser-output'})
wikip = wiki_box.find('p')
wiki = wikip.text.strip()
print wiki
Awi 是 wiki 站点的关键字。但是,如果该站点不存在怎么办。有人可以帮忙吗?
使用 try&except
import urllib2
try:
urllib2.urlopen(quote_page)
except urllib2.HTTPError as e:
print(e.code)
except urllib2.URLError as e:
print(e.args)
您需要获取请求状态码,看是否满足HTTP CODE 200
quote_page = "https://en.wikipedia.org/wiki/" + Awi
page = urllib2.urlopen(quote_page)
if page.getcode() == 200:
soup = BeautifulSoup(page, 'html.parser')
wiki_box = soup.find('div', attrs={'class':'mw-parser-output'})
wikip = wiki_box.find('p')
wiki = wikip.text.strip()
print(wiki)
else:
print("Page could not be loaded")