明天抢幻16,今天写了两个抢票脚本,来源是github
https://github.com/8090shuo/jd_maotai
上传两个py文件留作记录
在写脚本爬虫的过程中让我印象最深刻的是selenium中对应页面元素的定位。
def run(self):
tag = 1
self.driver.get(self.item_url)
Timer().start()
while True:
self.driver.get(self.item_url)
#first_result = self.wait.until(presence_of_element_located((By.XPATH, '//*[@id="product_information"]/div[3]/div[2]/ul/li[3]/button')))
first_result = self.wait.until(presence_of_element_located((By.CLASS_NAME, "btn-import")))
text_content = first_result.get_attribute("textContent")
print(text_content)
if text_content in ['等待抢购', '0元预约']:
print('还没开始,等待抢购!{}'.format(tag))
elif text_content == '立即购买':
try:
print('终于开始了,立即抢购!')
first_result.click()
#first_result = self.wait.until(presence_of_element_located((By.XPATH, '//*[@id="order_price"]/div/p/button')))
first_result = self.wait.until(presence_of_element_located((By.CLASS_NAME, "btn-import")))
first_result.click()
print("提交订单!")
time.sleep(30)
break
except common.exceptions.TimeoutException as e:
print('没赶上,明天再来吧!')
break
time.sleep(random.randint(1, 3) * 0.1)
tag += 1
if tag > 300:
break
比较通用的是By.XPATH 制作自动化工具,但是class name 和 ID定位得会更加快一点
系列文章:网上都是
这里是今天的leetcode 题目
要实现堆排序
现在我先手撕一遍熟悉熟悉
def sift(li,low,high): #low 是根节点索引, high是最后一个元素
i = low
j = 2*i+1
tmp = li[low]
while j<=high:
if j+1<=high and li[j+1]>li[j]:
j= j+1
if li[j]>tmp:
li[i] = li[j]
i=j
j=2*j+1
else:
break
li[i] = tmp
#sift 是一个将大顶堆的堆顶元素向下移动的操作
def heap_sort(li):
n= len(li)
for i in range(n//2-1,-1,-1):
sift(li,i,n-1)
#这样我们就构建了一个大顶堆
#下面用这个大顶堆做一个排序
for i in range(n-1,-1,-1):
li[0],li[i] = li[i],li[0]
sift(li,0,i-1)
return li
大顶堆排序 的时间复杂度: sift 操作是 O(logn)
heap_sort 是 O(n*logn)
注意建堆的时间复杂度为n!!!!
![](https://img-blog.csdnimg.cn/img_convert/3b36ce5b7de77cf9b83a1d84ac6f4dcf.png)
![](https://img-blog.csdnimg.cn/img_convert/357a22007dc5528703f0b234e9550e67.png)