作者: 42
郭:于謙的父親王老爺子是個心地善良的富人,放出話來“別的地方我管不了,我方圓十里內不能有窮人”。然後,他把附近的窮人都趕走了。“我心善,見不了窮人”。
前兩天寫了個腳本通過https://freeapi.ipip.net/獲取IP信息,在公司電信網絡沒問題,在家裡有時可以有時不行。對比dns後發現獲得的ip是不同的,於是打算在hosts文件里寫死。安卓的hosts文件在/system/etc/hosts,但是在/etc/hosts也有鏈接,獲取Termux已經在root用戶下,仍然提示文件只讀。搜索後發現我的android6用的可能是system-headless root,也就是/system是已只讀方式掛載的,這樣或許可以保護系統吧。解決辦法在How to edit 'etc/hosts' file?找到:
#for Android 6 su # become the root (don't miss confirmation request!) mount -o remount,rw /system # allow to write vi /system/etc/hosts ## edit the file in place - do what you want, then <ESC>:wq ## mount -o remount,ro /system # get things back to normal exit # unroot #for Android 8 with magisk and busybox su # become the root (don't miss confirmation request!) busybox mount -o remount,rw /system # allow to write vi /system/etc/hosts ## edit the file in place - do what you want, then <ESC>:wq ## busybox mount -o remount,ro /system # get things back to normal exit # unroot #for Android 10 #remount /system may get this error #mount: '/system' not in /proc/mounts #just remount the root dir / will be ok su # become the root (don't miss confirmation request!) mount -o remount,rw / # allow to write vi /system/etc/hosts ## edit the file in place - do what you want, then <ESC>:wq ## mount -o remount,ro /system # get things back to normal exit # unroot
後來呢,發現有時還時不行,所以不單單是dns的問題。再後來,發現ip-api家的免費接口也很不錯,現在兩個一起用。
本文更新於 2021/07/13。
網頁版使用了moment.js。家裡的菲利普電飯鍋的預約功能需要輸入幾個小時後做好飯,而不是幾點幾分做好飯,每次都要扳着指頭數距離明天早上7點還有幾個小時。下面還有一個python版,可以放到termux里跑:
#!/data/data/com.termux/files/usr/bin/python
# -*- coding: utf8 -*-
#時間比較
import sys,datetime
#str轉換成datetime
def strTodaTime(s):
if len(s)==19:
return datetime.datetime(int(s[0:4]),int(s[5:7]),int(s[8:10]),
int(s[11:13]),int(s[14:16]),int(s[17:19]))
else:
print("格式錯誤,--help查看幫助。")
#計算時間
def showTimeDuration(dtStr2='',dtStr1=''):
if dtStr1 == '':
datetime1 = datetime.datetime.now()
else:
datetime1 = strTodaTime(dtStr1)
if dtStr2 == '':
date2 = datetime.date.today() + datetime.timedelta(days=1)
time2 = datetime.time(7, 0)
datetime2 = datetime.datetime.combine(date2, time2)
else:
datetime2 = strTodaTime(dtStr2)
print(datetime2 - datetime1)
if __name__ == '__main__':
if len(sys.argv) == 3 :
showTimeDuration(sys.argv[1],sys.argv[2])
elif len(sys.argv) == 2 :
if sys.argv[1]=='--help':
print('參數1默認是第二天早上七點,如2017-11-16T07:00:00。\n參數2默認是當前時間,格式同參數一。')
else:
showTimeDuration(sys.argv[1])
else:
showTimeDuration()
蘋果X發售時間是15:00,阿迪達斯新鞋搶購發售時間是16:00,雙十一搶購是00:00。
我不是蘋果的用戶(索尼粉),也不是阿迪的用戶(迪卡儂粉),淘寶……
香港是國際大都市,擁有更多的航線和更廉價的航班,所以我們出國基本都從香港走。下面是一些有用的網站:
- 香港乘車易,官方提供的公共交通查詢網站,好用。
- B3X,從深圳灣口岸到屯門市中心,首班車06:45,標稱15分鐘一班,高峰期經常彈性增加班次,票價11元,約24分鐘。
- B3,從深圳灣口岸經屯門市中心到屯門碼頭,首班車06:55,20或30分鐘一班,票價11元,到屯門市中心約20分鐘,到屯門碼頭約40分鐘。
- 富裕小輪時刻表,從屯門碼頭到東涌,通常25分鐘18元。
- S56,從東涌碼頭到機場,首班車05:50,通常15分鐘一班,票價3.8元,約15分鐘。
- E33,屯門市中心到機場,首班車05:00,十分鍾左右一班,票價13.9元,約70分鐘。還有一個A33,也是從屯門市中心到機場,票價27.7元,約80分鐘。
所以既然是趕飛機的話,早上需要約個出租車以在06:30到達關口,然後趕上06:45或06:55去屯門市中心的巴士,然後約07:20從屯門市中心坐E33去機場,加20分鐘堵車時間(有遇到過),09:00到達機場。如果飛機是10:00的,應該是沒問題的。
如果坐到屯門碼頭坐船,07:40到碼頭,趕上08:00的船,08:30到東涌碼頭,轉S56(或坐出租車(記得給小費))差不多也是09:00到達機場。
其實還有一種辦法,可以趕上更早的飛機,就是從蛇口碼頭直接坐船到香港機場,無奈搜了半小時,找不到蛇口碼頭的網站,只知道票價較貴,時間很短。
記錄一些常用命令(待更新)。
from selenium import webdriver
#等待
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
#鍵盤
from selenium.webdriver.common.keys import Keys
#select元素
from selenium.webdriver.support.ui import Select
##配置並開啟火狐
firefox_profile = webdriver.FirefoxProfile()
#語言設置為zh-CN,en-US
firefox_profile.set_preference('intl.accept_languages','zh-CN')
#UA設置為iphone6 plus
firefox_profile.set_preference("general.useragent.override", "Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1")
#2關閉瀏覽器圖片
firefox_profile.set_preference('permissions.default.image', 2)
driver = webdriver.Firefox(firefox_profile=firefox_profile)
#窗口大小
driver.set_window_size(414,736)
#窗口最大化,可解決一些元素因窗口太小為顯示而無法點擊的情況
driver.maximize_window()
#頁面超時
driver.set_page_load_timeout(50)
##給火狐設置 http 或 socks 代理
firefox_profile = webdriver.FirefoxProfile()
proxy_ip = "127.0.0.1"
proxy_port = 18409
firefox_profile.set_preference("network.proxy.type", 1)
#http代理需要設置下面四行
firefox_profile.set_preference("network.proxy.http", str(proxy_ip))
firefox_profile.set_preference("network.proxy.http_port", int(proxy_port))
firefox_profile.set_preference("network.proxy.ssl", str(proxy_ip))
firefox_profile.set_preference("network.proxy.ssl_port", int(proxy_port))
#socks 代理只需設置下面兩行
firefox_profile.set_preference("network.proxy.socks", str(proxy_ip))
firefox_profile.set_preference("network.proxy.socks_port", int(proxy_port))
#可選
firefox_profile.set_preference("network.http.use-cache", False)
#可選
firefox_profile.update_preferences()
driver = webdriver.Firefox(firefox_profile=firefox_profile)
#在無界面模式下運行 headless
from selenium.webdriver.firefox.options import Options as FirefoxOptions
options = FirefoxOptions()
options.add_argument("--headless")
driver = webdriver.Firefox(firefox_profile=firefox_profile,options=options)
##driver的完整初始化參數
webdriver.Firefox(firefox_profile=None, firefox_binary=None,
capabilities=None, proxy=None,
executable_path=DEFAULT_EXECUTABLE_PATH, options=None,
service_log_path=DEFAULT_SERVICE_LOG_PATH,
service_args=None, service=None, desired_capabilities=None,
log_path=DEFAULT_LOG_PATH, keep_alive=True)
#https://github.com/SeleniumHQ/selenium/blob/trunk/py/selenium/webdriver/firefox/webdriver.py#L44
##頁面操作
#打開頁面
try:
driver.get("https://reportaproblem.apple.com")
except:
try:
#長時間沒加載完頁面時,可能是卡在了某個資源,按下Esc停止等待
driver.find_element_by_xpath('//body').send_keys(Keys.ESCAPE)
except:
return 'fail'
#有時鏈接會打開新窗口,切換到新窗口
driver.switch_to_window(driver.window_handles[-1])
#當前頁面鏈接
driver.current_url
#當前頁面標題
driver.title
#等待元素出現
try:
WebDriverWait(driver, 60).until(lambda the_driver: the_driver.find_element_by_xpath("//iframe[@id='aid-auth-widget-iFrame']").is_displayed())
except:
pass
##定位元素
#frame
driver.find_element_by_xpath("//iframe[@id='aid-auth-widget-iFrame']")
#ID
driver.find_element_by_id("appleId")
#根據屬性定位元素
captchaInput = driver.find_element_by_xpath("//input[@placeholder='键入图中的字符']")
#獲取輸入框中的值
inputValue = captchaInput.get_attribute('value')
#向輸入框中寫入字符
captchaInput.send_keys("你好")
#清空輸入框
captchaInput.clear()
#按下Tab鍵
driver.find_element_by_xpath('//body').send_keys(Keys.TAB)
capDivImg=driver.find_element_by_xpath("//div[@class='form-cell']//img")
#獲取圖片的鏈接
imgSrc=capDivImg.get_attribute('src')
#點擊包含“繼續”的按鈕
driver.find_element_by_xpath("//button[contains(text(),'继续')]").click()
#根據CSS定位button
driver.find_element_by_xpath("//button[@class='button button-primary last nav-action']")
#點擊值為“步行”的選項
driver.find_element_by_xpath("//option[text()='步行']").click()
#定位一組元素
labels = driver.find_elements_by_css_selector("label.labelClass")
answerInputs = driver.find_elements_by_xpath("//input[@placeholder='答案']")
questions = []
for label in labels:
questions.append( label.get_attribute('innerHTML') )
#等待包含特定字符的元素
WebDriverWait(driver, 3).until(lambda the_driver: the_driver
.find_element_by_xpath("//p[@class='subtitle content-item tk-label'][contains(text(),'已超时')]")
.is_displayed())
#獲取select元素
typeS = Select(driver.find_element_by_id("searchDropdownBox"))
#根據值來選中select元素
typeS.select_by_visible_text('行山')
#獲取元素顯示的文字
driver.find_element_by_xpath("//tr[@class='updateblock']//td[@class='timer']").get_attribute("innerText")
driver.find_element_by_xpath("//tr[@class='updateblock']//td[@class='timer']").get_attribute("innerHTML")
命令行啟動瀏覽器
firefox -width 1440 -height 960 google-chrome --window-size="1440,960"
本文更新於 2024/09/01。