一.格式化傳參
1.定義一個字符串 base_url='http://www.python.com/?page={}'
2.請將數字1 格式化傳遞到base_url
代碼:
base_url='http://www.python.com/?page={}'
base_url=base_url.format(1)
print(base_url)
輸出結果:
http://www.python.com/?page=1
二.循環 格式化傳參
1.定義一個變量list_a=range(1,10) #range函數以后會經常用到 大家百度用法
2.請對list_a進行 循環 打印它的每一個元素
3.請在上述循環里定義一個變量req_url,req_url的值應為list_a的每個元素格式化傳參到base_url后的值 并打印輸出req_url
代碼:
list_a=range(1,10)
base_url='http://www.python.com/?page={}'
from collections import Iterable
print(isinstance(list_a,Iterable))
for i in list_a:
req_url=base_url.format(i)
print(i,req_url)
輸出結果:
True
1 http://www.python.com/?page=1
2 http://www.python.com/?page=2
3 http://www.python.com/?page=3
4 http://www.python.com/?page=4
5 http://www.python.com/?page=5
6 http://www.python.com/?page=6
7 http://www.python.com/?page=7
8 http://www.python.com/?page=8
9 http://www.python.com/?page=9
三.字符串分割 列表索引
1.定義一個字符串 a='I like python'
2.請觀察a的特點,對其進行分割 變量b存儲分割后的值
3.對變量b進行循環并打印每個元素的值
4.循環打印b的每一個索引和索引對應的值
代碼:
a='I like python'
b=a.split(' ')
print(b,type(b))
from collections import Iterable
print(isinstance(b,Iterable))
for i in b:
print(i)
for i in enumerate(b):
print(i)
for index,value in enumerate(b):
print(index,value)
輸出結果:
['I', 'like', 'python'] <class 'list'>
True
I
like
python
(0, 'I')
(1, 'like')
(2, 'python')
0 I
1 like
2 python
四.索引切片
1.取出b當中的like,命名變量c
2.取出b當中的'th'命名變量d
3.取出b當中的python 命名變量e,并判斷d是否存在e當中 存在輸出'存在',不存在輸出'不存在'
代碼:
c=b[1]
print(c)
d=b[2][2:4]
print(d)
e=b[2]
print(e,type(e))
if d in e:
print('存在')
else:
print('不存在')
輸出結果:
like
th
python <class 'str'>
存在
五.循環判斷
1.使用startswith函數 判斷e是否以d開頭 條件為真輸出'是',為假輸出'不是'
代碼:
a='I like python'
b=a.split(' ')
c=b[1]
d=b[2][2:4]
e=b[2]
if e.startswith(d):
print('是')
else:
print('不是')
輸出結果:
不是
2.對list_a進行循環,并且判斷如果list_a元素值<6,打印輸出'<6',>=6 打印輸出'>=6'
代碼:
list_a=range(1,10)
for i in list_a:
if i<6:
print("<6")
else:
print(">=6")
輸出結果:
<6
<6
<6
<6
<6
>=6
>=6
>=6
>=6
3.對list_a進行循環打印,如果元素值=6跳過循環
代碼:
for i in list_a:
if i==6:
continue
else:
print(i)
輸出結果:
1
2
3
4
5
7
8
9
4.對list_a進行循環打印,如果元素值>6 跳出循環
代碼:
for i in list_a:
if i>6:
break
else:
print(i)
輸出結果:
1
2
3
4
5
6
六.字典 json
1.定義空字典dict_a
2.利用循環將list_a的每個元素添加到dict_a
3.對dict_a進行循環 打印key和對應的value
4.講dict_a轉換為json格式
代碼:
dict_a={}
list_a=range(1,10)
for i in list_a:
dict_a[i]=i
for k,v in dict_a.items():
print(k,v)
import json
json_c=json.dumps(dict_a)
print(json_c,type(json_c))
輸出結果:
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
{"1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9} <class 'str'>
七.循環嵌套
附加題:循環嵌套打印99乘法表
代碼:
for i in range (1,10):
for j in range(1,10):
print(j,"x",i,"=",i*j,"\t",end="")
if i==j:
print("")
break
輸出結果:
1 x 1 = 1
1 x 2 = 2 2 x 2 = 4
1 x 3 = 3 2 x 3 = 6 3 x 3 = 9
1 x 4 = 4 2 x 4 = 8 3 x 4 = 12 4 x 4 = 16
1 x 5 = 5 2 x 5 = 10 3 x 5 = 15 4 x 5 = 20 5 x 5 = 25
1 x 6 = 6 2 x 6 = 12 3 x 6 = 18 4 x 6 = 24 5 x 6 = 30 6 x 6 = 36
1 x 7 = 7 2 x 7 = 14 3 x 7 = 21 4 x 7 = 28 5 x 7 = 35 6 x 7 = 42 7 x 7 = 49
1 x 8 = 8 2 x 8 = 16 3 x 8 = 24 4 x 8 = 32 5 x 8 = 40 6 x 8 = 48 7 x 8 = 56 8 x 8 = 64
1 x 9 = 9 2 x 9 = 18 3 x 9 = 27 4 x 9 = 36 5 x 9 = 45 6 x 9 = 54 7 x 9 = 63 8 x 9 = 72 9 x 9 = 81
【Python爬蟲作業】-第七次作業
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
推薦閱讀更多精彩內容
- 一:簡單敘述爬蟲原理答:從網頁上請求數據,下載數據,解析數據,從而得到自己想要的內容。二:利用chrome瀏覽器查...
- 求1-100以內的素數 思路:直接求素數的思路當時沒想好 ,就直接排除法將不是素數的從列表中刪除 爬取糗事百科頁面...
- fp=open('C:\\Users\\Administrator\\Desktop\\python\\text....