python 日常记录笔记

昨天跟大佬交流下,说要多写写博客。可没有这个耐心,平常都是记笔记,从来没写博客习惯。好了,废话不多说。直接上笔记!

python 笔记

在xpath获取内容元素去除括号
normalize-space

price = tree.xpath("normalize-space(//div//strong[@id='J_StrPrice']//em[@class='tb-rmb-num']/text())")

sql 语句加引号

good.title = '"%s"'%(title)

json添加数据

good = {'partnerId': 233035, 'poiId': 42367975, 'roomId': 127843856, 'goodsId': 624959748, 'roomName': '商务房B'}
good["checkInDate"] = "2020-04-17"
good['checkOutDate'] ="2020-09-18"
good['poiId'] = 123

去除空行

strip()

获取selenium按钮后标签值

age_index = str(driver.find_element_by_xpath(".//*[@id='rd-resumelist-pageNum']").text)
a.find_element_by_css_selector('div.js-component-component>span.nui-tree-item-text').click()

判断执行时间

start = time.time()
end = time.time() - start
print(end)

合并一对多

tables = {'name':'232','name':'wewe','val':'23232'}
dic = {}
for n in tables:
    t = ((n['value']),)
    if n['name'] not in dic:
        dic[n['name']] = t
    else:
        dic[n['name']] +=t

import datetime
import time

from time import strftime

当前日期

start_times = time.strftime('%Y%m%d',time.localtime(time.time()))

加7天

now_time = datetime.datetime.now()
end_times = (now_time + datetime.timedelta(days=7)).strftime("%Y%m%d")

print(start_times)
print(end_times)

时间戳转化年月日

import time
timeStamp = 1381419600
timeArray = time.localtime(timeStamp)
otherStyleTime = time.strftime("%Y-%m-%d ", timeArray)
print(otherStyleTime) 

房间具体参数

res_data = responses.json() 报错问题

解决美团post请求
你先获取text 在用json.loads

res_data = json.loads(responses.text)
request payload

  headerss = {
    'Content-Type': 'application/json'

}
urlss = 'https://ihotel.meituan.com/group/v1/yf/productDetail?_token='+token+'&userid=&utm_medium=touch&version_name=999.9&platformid=1'
responses = requests.post(url=urlss, data=jdata, headers=headerss)

房间具体参数

res_data = responses.json()

try:
    res_data = json.loads(responses.text)
    good_price = res_data['data']['sellPrice']
except Exception:
    res_data = 0
    good_price  = 10

点击js滑动,点击标签元素

js = "window.scrollTo(100,450)"
driver.execute_script(js)

driver.find_element_by_css_selector("div.loginForm>input#loginBtn").click()

关闭新标签页面

driver.close()

关闭浏览器

driver.quit()

中文解析 这个问题困了我好久,终于解决了

import urllib

s='%E6%A4%8D%E7%89%A9%E5%85%B1%E4%BA%AB'
s=urllib.parse.unquote(s)
print(s)
//unicode
ddd=u'\u9a71\u868a\u6c34\u6237\u5916\u55b7\u96fe\u6301\u4e45\u9632\u868a\u866b\u53ee\u54ac\u513f\u7ae5\u5b9d\u5b9d\u9a71\u868a\u6db2\u5bb6\u7528\u6cf0\u56fd\u539f\u6599\u82b1\u9732\u6c34'
ddd=ddd.encode("utf-8").decode("utf-8")


解析域名 eg:淘宝,天猫,京东,拼多多

from urllib.parse import urlparse
def get_host(url):
    url_dic = urlparse(url)
    return url_dic.netloc
  

接口

https://i.meituan.com/awp/h5/hotel/poi/deal.html?poiId=42367975&cityId=184&startTime=1586995200000&endTime=1587081600000&ste=_b400201&ct_poi=066165667419427744021571665568740526499_c0_e18045777570585517042_a%25e9%25a1%25b9%25e7%258e%258b%25e6%2595%2585%25e9%2587%258c_b400201_o1_dhotelpoitagb_k1002&type=1&zlFlag=true

美团好评

https://ihotel.meituan.com/api/v2/comments/biz/score?poiId=42367975

美团详情

https://ihotel.meituan.com/group/v1/poi/42367975?utm_medium=touch&version_name=999.9&platformid=1&start=1586995200000&end=1587081600000&mypos=33.469098%2C119.780797&lat=33.469098&lng=119.780797&gpsCityId=181&cityId=184&subtype=0&type=1&isRecommend=0&propagateData&recType=0&isLocal=1&entryType=2&fields=phone%2CscoreSource%2CmarkNumbers%2CcityId%2Caddr%2Clng%2ChasGroup%2CsubwayStationId%2Ccates%2CfrontImg%2CchooseSitting%2Cwifi%2CavgPrice%2Cstyle%2CfeatureMenus%2CavgScore%2Cname%2CparkingInfo%2Clat%2CcateId%2Cintroduction%2CshowType%2CareaId%2CdistrictId%2Cpreferent%2ClowestPrice%2CcateName%2CareaName%2CzlSourceType%2CcampaignTag%2CmallName%2CmallId%2CbrandId%2CbrandName%2CbrandLogo%2CbrandStory%2CsmPromotion%2Cktv%2Cgeo%2ChistoryCouponCount%2Crecommendation%2CiUrl%2CisQueuing%2CnewPayInfo%2CpayInfo%2CsourceType%2Cabstracts%2CgroupInfo%2CisSuperVoucher%2Cdiscount%2CisWaimai%2CcollectionDeals%2CnearPoi%2Csparkle%2CtopicList%2CcityIds%2CshowChannel%2CshowStatus&_token=eJxVUdmuozgU%2FBekyQvRxQZsw5WiUfZAQlayQKsVsS9hC5hwk9b8%2Bzg9fR%2FGD3WqyiXrHJ9fXK353CcE7Mh97hHU3CcHP8AH5vocbdgNUgiQREAUrIA%2B5%2F3fUyTS59z6NOE%2Bf0hQ7hMEfr6NPdM%2FIJJwX8Hyz%2F4fKjIqyv134VyNRbiY0qr5FITkIw8S2jrFh1fmgtNVQoyEuKRBJlRlIviBk33ENM%2F%2BZkrzB7IoYaIS1PMS%2BmQaKnKvoU5NzSQPBqw9rKpIfM8EekHhf7sEKBD%2F5zY0GFxdGQARwJ5Hr%2BzdAcAYYoQxkaEqi4TIMhAhIhBjhLBCZIBELKvq1QPXACpARoQQRABSEIIEyOLV%2BUtEgcrAgQzcNwsIAyV4g%2FuWmIGK3hJ9h5XfEe%2B7m2sJr%2F7v0VlP1Inc641tR%2BzRZ8WG6L2yWeZEA1q3AdsQx34yN9lPsnr7U50%2FlX5rg62UZZskKhgL9K%2F18QYf4mtoCAdF5ae7o58KTy8cViPLGp7txVq62LltWOdl2Y6Xk7ALH6CsjosiAkV5sPhtSrrXOpHIIwei%2BXVCqce7vgurU3Hh1bE5Q2egwwk62qBts5djkGEx9ZaHDRIjTS9fj1pYgzKJO3u3bfgu3Uxe4yTUGmWEJsg35tkBRU1AYGzoi%2BHG1be8eBsaL23lm1Z2x0qr5t1oK9spNcqHc5lObUUDe6HahJVj%2BKfbKNA7LYrqnS1RwS7G1rgOzs34HvBqcUuSYXqRtDTSZ7vOR8vjw7k5Vgyqe0Kj1lgd3Or8OhweuQmEpZGFvijKYJR4Q1qPHvw8GqXxxW330%2FFR2YcXfjU6pHa0C3g7k5JWGxlfSmTOpOKBd8bciuNTlmuEWk9VyE5H3dia1cqoYLlQZ3to8ebCLJbS058UyTErtwWc86eNrrfgYI40S4l3uosDe6iY4da%2BH6zUHGdpdRSccQCsdYSWd6xOJ%2FV%2BGidbdzp%2Fvl53%2FcuYO1FXq8XKOnqSlwY3vkWLwmjgfnJbQ9qievksp%2Bs2tYutqKj0cs6mYhMueD48UYpbnAHHv%2Beijx9r4cQ0znV3pHThMJyNtcVisV%2BuUNLV%2FiFv7jHUd1n6LFtFzHG4wHhVb9pLnUf6yweCqh7NWzcYcP%2F8C2lBVj4%3D
https://ihotel.meituan.com/group/v2/poi/detail/service?poiId=42367975&utm_medium=touch

美团房间类型

https://ihotel.meituan.com/group/v1/poi/42367975/imgs?utm_medium=touch&version_name=999.9&platformid=1&classified=true
https://ihotel.meituan.com/group/v1/yf/list/175465308?utm_medium=touch&version_name=999.9&platformid=1&start=1586966400000&end=1587052800000&type=0&lat=34.240466&lng=118.853476&gpsCityId=184&cityId=184&poi=244720&iuuid=6976D60EF7B1A64CEFBFD4A4811282B070569691AB4E1A22479FC6907B4BC5E2&propagateData&_token=eJyrVi/yTFG3UlBX11FQLykGsgxNLcwNjCwMDU2NjCyAgslIopZw0aSiMBegcHQsmB0EZ3rCWLkhMFY2nJUIZ5XAWcWZ6Xkg+1O9yl0cwcDVVr0WAJFCJME=

拼多多

https://youhui.pinduoduo.com/search/landing?keyword=%E4%B8%B8%E7%BE%8E
http://api-gw.haojingke.com/index.php/v1/api/pdd/goodsdetail?apikey=08bcbce85b984d71&goods_id=39461888578

python 动态获取xpath

xpath0="//*[@id="house-lst"]/li[{}]"
for i in range(1,31):
    xpath=xpath0.format(i)

fastapi
然后执行命令:

uvicorn main:app --reload

119.45.43.107:8000/items/7524813?q=somequery
http://127.0.0.1:8000/items/7524813?q=somequery

PIL报错,卸载重装

python -m pip uninstall Pillow
python -m pip install Pillow

selenium iframe问题 textarea写入文本

dr.switch_to.frame('ueditor_0')
dr.find_element('tag name', 'body').send_keys('hello')

亲测有效,一招解决错误:This application failed to start because not Qt platform plugin could be initialized.

变量名:QT_QPA_PLATFORM_PLUGIN_PATH
变量值:D:\soft\Anaconda\Lib\site-packages\PySide2\plugins\platforms
不同用户,变量名一致,变量值随文件位置变。
最后,保存变量后,一定要重启电脑,因为新的环境变量要重启后才能被系统识别!

python 自动化相关知识

安装python3.7+
安装uiautomator2
pip install --pre uiautomator2

pip install pillow

###初始化
python -m uiautomator2 init

1、安装:

pip install --pre --upgrade weditor
2、使用:

python -m weditor

这个意思整个重启adb devices

adb start-server
发现无法连上,出现offline。可以尝试adb kill-server,然后再用adb device查看

查看手机app包名称
adb shell am monitor

d = u2.connect(‘3395b086’) #括号内为adb devices获取的设备号

常用的定位方式:

ResourceId定位:
d(resourceId=“com.meizu.mzbbs:id/tp”).click()

Text定位:
d(text=“精选”).click()

Description定位:
d(description="…").click()

ClassName定位:
d(className=“android.widget.TextView”).click()

任务计划软件命令

taskschd.msc

猜你喜欢

转载自blog.csdn.net/weixin_37254196/article/details/107997002