python學習筆記 第十一章

2021-08-13 07:09:15 字數 1587 閱讀 4256

# import library files

import requests

# 傳送乙個http請求 send http requests

res = requests.get('')

# check error 檢查錯誤

res.raise_for_status()

# check state code 檢驗狀態碼

print(res.status_code == requests.codes.ok)

print(len(res.text))

# print the text of less then 250

print(res.text[:250])

# open a file

playfile = open('romeoandjuliet.txt', 'wb')

# iter_content()方法在迴圈的每次迭代中,返回一段內容。每一段都是 bytes 型別,你需要指定一段包含多少位元組。

for chunk in res.iter_content(100000):

# write text

playfile.write(chunk)

playfile.close()

# requests 抓取的是網頁的原始資料,如果不是txt檔案的話就會將html等資料也抓取下來

import requests

res = requests.get('')

try:

res.raise_for_status()

except exception as exc:

print('there was a problem: %s' % (exc))

import requests, bs4

res = requests.get('')

res.raise_for_status()

nostarchsoup = bs4.beautifulsoup(res.text, "lxml")

examplefile = open('example.html')

examplesoup = bs4.beautifulsoup(examplefile, "lxml")

print(nostarchsoup)

import requests, bs4

examplefile = open('example.html')

examplesoup = bs4.beautifulsoup(examplefile.read(), "lxml")

# 用 select()方法尋找元素

elems = examplesoup.select('#author')

print(type(elems))

print(len(elems))

print(str(elems[0]))

print(elems[0].attrs)

pelems = examplesoup.select('p')

print(str(pelems[0]))

print(pelems[0].gettext())

Android學習筆記 第十一章

第十一章 使用對話方塊 11.2探索不同型別的對話方塊 android自帶的對話方塊 dialog 所有對話方塊的基類 alertdialog 帶有1,2,3個按鈕的控制項 characterpickerdialog 用於顯示和選擇與基本字元相關聯的聲調字元的dialog datepickdialo...

第十一章python作業

11 1 城市和國家 編寫乙個函式,它接受兩個形參 乙個城市名和乙個國家名。這個函式返回乙個格式為city,country 的字串,如santiago,chile 將這個函式儲存在乙個名為city functions.py的模組中。建立乙個名為test cities.py的程式,對剛編寫的函式進行測...

第十一章 python模組

11.1 模組的概述 modules 可以看作是函式的集合體 11.2 自定義模組 兩個作用,規範 方便其他程式呼叫,提高開發效率 11.2.1 建立模組 正常書寫,儲存以 py 結尾的,就是模組了 11.2.2 使用import語句匯入模組 import modules 1,modules 2 a...