Day 06 流程控制和爬虫基础2
目录
if 判断
单分支结构
age = int(input('please enter your age: '))
if age > 18:
print('adult aready')
双分支结构
age = int(input('please enter your age: '))
if age >= 18:
print('adult aready')
else:
print('just grow up')
多分支结构
age = int(input('please enter your age: '))
if age >= 18:
print('adult aready')
elif age >= 13:
print('teenager')
else:
print('just grow up kid')
for循环
for循环的基本用法
# 把列表内部的值循环打印
for d in range(1, 32):
print(f'{d}日')
for循环嵌套
# 内层循环全部走完,才会走一次外层循环,每次走入外层循环,内层循环都会重置
for m in range(1, 13):
for d in range(1, 32):
print(f'{m}月{d}日')
break
# 循环提前被中断
m = 2
for d in range(1, 32):
if d == 29:
break
print(f'{m}月{d}日')
continue
# 跳出本次循环,不执行下面的代码,然后直接进入下一次循环
for m in range(1, 13):
for d in range(1, 32):
if m == 2 and d > 28:
continue
print(f'{m}月{d}日')
爬虫基础2
爬取豆瓣TOP250
import requests
import re
# 获取所有电影的url
num = 0
for i in range(10):
url = f'https://movie.douban.com/top250?start={num}&filter='
num = num + 25
# 发送请求
res = requests.get(url = url)
data = res.text
# 解析数据
movie_list = re.findall('<div class="item">.*?<a href="(.*?)">.*?<span class="title">(.*?)</span>.*?<span class="rating_num" property="v:average">(.*?)</span>', data, re.S)
# 保存数据
with open('douban TOP250.txt', 'a', encoding='utf-8') as f:
for line in movie_list:
movie_name = line[1]
movie_rating = line[2]
movie_detail_web = line[0]
movie_data = f'''
电影名称: {movie_name}
电影评分: {movie_rating}
电影详情: {movie_detail_web}
'''
f.write(movie_data)
print('爬取成功,程序结束')
爬取豆瓣数据接口(异步数据)
import requests
import json
# 分析目标网址的异步请求返回的数据(接口)
# f12 -- network -- XHR -- headers -- Request URL
# 发送请求
num = 0
for i in range(10):
url = f'https://movie.douban.com/j/chart/top_list?type=5&interval_id=100%3A90&action=&start={num}&limit=20'
num = num + 20
res = requests.get(url = url)
# json 是一种第三方的数据格式
# json.dumps() 将python数据格式转换为json数据格式
# json.loads() 将json数据格式转化为python数据格式
movie_list = json.loads(res.text)
# 保存数据
for movie_dic in movie_list:
movie_name = movie_dic['title']
movie_rating = movie_dic['rating'][0]
movie_rank = movie_dic['rank']
movie_regions = movie_dic['regions']
movie_release_date = movie_dic['release_date']
movie_url = movie_dic['url']
movie_data = f'''
电影名称: {movie_name}
电影评分: {movie_rating}
电影排名: {movie_rank}
拍摄国家: {movie_regions}
电影年代: {movie_release_date}
电影详情: {movie_url}
'''
with open('douban TOP 200 action movies', 'a', encoding='utf-8') as f:
f.write(movie_data)
print('爬取成功,程序结束')
爬取电话号码(反爬破解基础)
import requests
import re
url='http://qiche.huangye88.com/xinxi/s121quumg56853.html'
# 反爬策略,通过User-Agent判断是否是浏览器
# f12 -- Network -- All -- Name第一行 -- Headers -- Request Headers -- User-Agent
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36'}
# 发送请求
res = requests.get(url = url, headers = headers)
data = res.text
# 解析数据
phone_num_list= re.findall('<ul class="pro">.*?<li><label>手<span class="none">占</span>机</label><h3 class="big">(.*?)</h3></li>.*?</ul>',data, re.S)
# 输出数据
phone_num = phone_num_list[0]
print(phone_num)