简单爬取“最好大学网2018大学排名”,爬取结果存入指定 目录下txt文件中
爬取对象 url = http://www.zuihaodaxue.cn/zuihaodaxuepaiming2018.html
工具:python3.6 IDLE
程序总体上也是分为三步:一、requests.get()获取url信息,并返回r.text;二、用BeautifulSoup解析r.text内容,后去标签中的信息,将爬取到的类似于 “大学名称”、“省份”、“排名”等信息用存入 ulist 列表中;三、对存入ulist列表中的信息进行:打印、输出、保存等进一步详细的操作。
附上程序所有代码:(同样生成了.exe程序文件)
import requests
from bs4 import BeautifulSoup
import bs4
import os
def getHTMLText(url):
try:
r = requests.get(url, timeout = 30)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
return ""
def fillUnivList(ulist, html):
soup = BeautifulSoup(html, "html.parser")
for tr in soup.find('tbody').children:
if isinstance(tr, bs4.element.Tag)