作业①:
要求:在中国气象网(http://www.weather.com.cn)给定城市集的7日天气预报,并保存在数据库。
实验步骤:
1、观察url
福州:
上海:
2、F12寻找所需数据

3、写出正则表达式
date = re.findall(r'<h1>(.*?)</h1>', req.text)#日期message = re.findall(r'<p title="(.*?)"', req.text)#天气信息temp1 = re.findall(r'<span>(\d+)</span>', req.text) temp2 = re.findall(r'<i>(.*?)</i>', req.text)#气温
4、存储到数据库
点击查看代码class WeatherDB:
def openDB(self):
self.con=sqlite3.connect("weathers.db")
self.cursor=self.con.cursor()
try:
self.cursor.execute("create table weathers (wCity varchar(16),wDate varchar(16),wWeather varchar(64),wTemp varchar(32),constraint pk_weather primary key (wCity,wDate))")
except:
self.cursor.execute("delete from weathers")
def closeDB(self):
self.con.commit()
self.con.close()
def insert(self, city, date, weather, temp):
try:
self.cursor.execute("insert into weathers (wCity,wDate,wWeather,wTemp) values (?,?,?,?)",
(city, date, weather, temp))
except Exception as err:
print(err)
def show(self):
self.cursor.execute("select * from weathers")
rows = self.cursor.fetchall()
print("%-16s%-16s%-32s%-16s" % ("city", "date", "weather", "temp"))
for row in rows:
print("%-16s%-16s%-32s%-16s" % (row[0], row[1], row[2], row[3]))
输出信息:
1、控制台输出

2、数据库内容

心得体会:
1、更好地掌握了正则表达式的使用
2、学会该如何将数据存储到数据库,并查看数据库内容
码云链接:https://gitee.com/huang-weiting/data-acquisition/blob/master/%E4%BD%9C%E4%B8%9A2/weather.py
作业②
要求:用requests和BeautifulSoup库方法定向爬取股票相关信息。
候选网站:东方财富网:http://quote.eastmoney.com/center/gridlist.html#hs_a_board
技巧:在谷歌浏览器中进入F12调试模式进行抓包,查找股票列表加载使用的url,并分析api返回的值,并根据所要求的参数可适当更改api的请求参数。根据URL可观察请求的参数f1、f2可获取不同的数值,根据情况可删减请求的参数。
参考链接:https://zhuanlan.zhihu.com/p/50099084
实验步骤:
1、F12观察网络状态,找到所需信息

f12/f14/f2/f3/f4/f5/f6/f7/f15/f16/f17/f18就是我们需要的信息
2、获取单个页面数据
点击查看代码def getOnePageStock(cmd,page):
data = getHtml(cmd,page)
#print(data)
db = MoneyDB()
db.openDB()
datas = data[0].split('},')
infos=[]
for info in datas:
if info[-1]!='}':
info=info+'}'
infos.append(json.loads(info))
stocks = []
for stock in infos:
stocks.append([stock['f12'],stock['f14'],stock['f2'],stock['f3'],stock['f4'],stock['f5'],stock['f6'],stock['f7'],stock['f15'],stock['f16'],stock['f17'],stock['f18']])
db.insert(stock['f12'],stock['f14'],stock['f2'],stock['f3'],stock['f4'],stock['f5'],stock['f6'],stock['f7'],stock['f15'],stock['f16'],stock['f17'],stock['f18'])
#print(stocks)
db.closeDB()
return stocks
3、获取全部数据
点击查看代码cmd = {
"上证指数":"C.1",
"深圳指数":"C.5",
"沪深A股":"C._A",
"上证A股":"C.2",
"深圳A股":"C._SZAME",
"新股":"C.BK05011",
"中小板":"C.13",
"创业板":"C.80"
}
for i in cmd.keys():
page = 1
stocks = getOnePageStock(cmd[i],page)
#自动爬取多页,并在结束时停止
while True:
page +=1
if page>8:
break
if getHtml(cmd[i],page)!= getHtml(cmd[i],page-1):
stocks.extend(getOnePageStock(cmd[i],page))
print(i+"已加载第"+str(page)+"页")
else:
break
df = pd.DataFrame(stocks)
4、存储到数据库同作业1,只要改变一些参数即可
输出信息:
1、控制台输出

2、数据库内容

心得体会:
1、进一步了解了不同网站该如何爬取信息数据
2、巩固了翻页处理
码云链接:https://gitee.com/huang-weiting/data-acquisition/blob/master/%E4%BD%9C%E4%B8%9A2/money.py
作业③:
要求: 爬取中国大学2021主榜 https://www.shanghairanking.cn/rankings/bcur/2021
所有院校信息,并存储在数据库中,同时将浏览器F12调试分析的过程录制Gif加入至博客中。
技巧: 分析该网站的发包情况,分析获取数据的api
实验步骤:
1、分析该网站的发包情况

2、寻找正确url
url = "https://www.shanghairanking.cn/_nuxt/static/1632381606/rankings/bcur/2021/payload.js"
3、编写正则表达式
name = re.findall(r'univNameCn:"(.*?)"', Data)#学校名称score = re.findall(r'score:(.*?),', Data)#总分
4、存储到数据库同作业1,只要改变一些参数即可
输出信息:
1、控制台输出

2、数据库内容

心得体会:
1、运用js抓包,相比于之前的抓包方式更简洁,不需要额外进行翻页处理
















