0%
除了现实中的蝇营狗苟,我的心中还住着星辰大海
关于导入
1 2 3
| from urllib.request import urlopen
|
range用法
1 2 3 4 5 6 7 8 9 10 11
| for i in range(1,5): print(i)
for i in range(1,5,2): print(i)
for i in range(5): print(i)
|
时间的用法
1 2 3 4 5 6 7 8 9 10 11 12 13
| import time print (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
import time for i in range(7): time.sleep(2) print(i)
import time print(time.time())
|
1 2 3 4 5 6 7 8 9 10 11 12
| def get_links_from(channel,pages): list_view = '{}/pn{}'.format(channel,str(pages)) wb_data = requests.get(list_view) time.sleep(1) soup = BeautifulSoup(wb_data.text,'lxml') if soup.find('td','t'): for link in soup.select('td.t > a.t'): item_link = link.get('href').split('?')[0] tb58.insert_one(item_link) print(item_link) else: pass
|
if else 结构
1 2 3 4 5 6 7 8 9 10 11
| number = 23 guess = int(input('请输入一个整数:')) if guess == number: print('恭喜,你猜对了。') print('(但你没有获得任何奖品!)') elif guess < number: print('不对,你猜的有点儿小') else: print('不对,你猜的有点大') print('完成')
|
pass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
以if语句为例,在c或c++/java中: if(true) ;//do nothing else { //do something }
if true: pass else:
|
map函数
1 2 3 4 5 6 7
|
def f(x): return x * x a = map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9]) print(list(a))
|
导入函数和变量
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
def haha(): print('haha') list = [1,2,3,4,5]
import demo test.haha() print(test.list)
from test import haha haha()
|