In[1]
# 安装beatifulSoup4
# beatifulSoup4是一个著名的爬虫框架
!pip install bs4
In[2]
# 安装openpyxl
# xlwt,xlrd是Python写Excel文件用的package.
# 有严重的兼容性问题, 在windows上请尽量使用VSTO!! 在Python 3.5环境中可凑活使用openpyxl
!pip install openpyxl
In[3]
#载入必要package
import sys
import time
from bs4 import BeautifulSoup
import re
import urllib
import openpyxl
In[4]
#定义一个方法, 获得页面全部内容
def askURL(url):
request = urllib.request.Request(url)#发送请求
try:
response = urllib.request.urlopen(request)#取得响应
html= response.read()#获取网页内容
# print (html)
except urllib.error.URLError as e:
if hasattr(e,"code"):
print (e.code)
if hasattr(e,"reason"):
print (e.reason)
return html
In[5]
#获取相关内容
#Warning: 解析页面的代码和页面结构强相关, 当页面html结构发生变化的时候必须同步升级, 否则当场作废
def