没有调用怎么能运行呢?加上最后的部分import urllib.request,urllib.error
def main():
baseurl="https://www.dytt8.net/html/gndy/dyzz/list_23_1.html"
# 1.爬取网页
datalist=getData(baseurl)
savepath=".\\电影天堂TOP250.xls"
saveData(savepath)
askURL("https://www.dytt8.net/html/gndy/dyzz/list_23_1.html")
#爬取网页
def getData(baseurl):
datalist=[]
for i in range(0,10): #调用获取页面信息的函数,10次
url = baseurl + str(i*25)
html = askURL(url) #保存获取到的网页源码
#2.逐一解析数据
return datalist
#得到指定一个url的网页内容
def askURL(url):
head={#模拟浏览器头部,向豆瓣服务器发送消息
"User-Agent": "Mozilla / 5.0(Windows NT 10.0;WOW64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 86.0 .4240 .198Safari / 537.36"
}
#用户代理表示告诉豆瓣服务器,我们是什么类型的机器
request = urllib.request.Request(url,headers=head)
html=""
try:
response = urllib.request.urlopen(request)
html = response.read().decode("gbk")
print(html)
except urllib.error.URLError as e:
if hasattr(e,"code"):
print(e.code)
if hasattr(e,"reason"):
print(e.reason)
return html
#保存数据
def saveData(savepath):
print("save...")
if __name__ == "__main__":
main()
|