爬虫训练----与网页进行表单交互的应用小题1

题目链接:http://www.shiyanbar.com/ctf/1971

开始我的思路是用爬虫提交表单,与服务器进行交互。

from selenium import webdriver
headers={
    'User-Agent': 'xxx',
    'Cookie': 'xxx',
    'Referer': 'xxx'
}
url='http://ctf5.shiyanbar.com/jia'
driver = webdriver.Chrome()
driver.get(url)
driver.implicitly_wait(0.5)
nums=driver.find_element_by_xpath('//*[@id="templatemo_content"]/form/div').text
b = eval(str(nums.replace('x', '*')))
pwd=driver.find_element_by_name('pass_key')
pwd.send_keys(b)
cill=driver.find_element_by_xpath('//*[@id="templatemo_content"]/form/input[2]').click()

这是本人的做法,但是越看越觉得繁杂,就去看了别人的writeup。

发现了一种新奇的玩意,session()

直接跟浏览器进行交互,用法跟平时的request差不多。

import requests
from bs4 import BeautifulSoup
url='http://ctf5.shiyanbar.com/jia/'
s = requests.session()
response = s.get(url)
soup = BeautifulSoup(response.text,'lxml')
buf = soup.find('div',attrs={'name':'my_expr'}).get_text()
buf = buf.replace('x','*')
response = s.post(url,data={'pass_key':eval(buf)})
print(response.content)

还有一种非常奇葩的做法,需要手速qwq。

用python监控剪切板,剪切板发生变化,立刻进行计算。

进入页面后,立刻选中表达式,然后在表单中粘贴,submit。过程要求一气呵成,手速要快。

但是通过这个也可以知道,python监控剪切板的使用方法。

import pyperclip
while True:
    d = pyperclip.paste()
    if d == '1':
        continue
    else:
        s = d.replace('x','*')
        ans = eval(s)
        pyperclip.copy(str(ans))
        break
原文地址:https://www.cnblogs.com/ZQUACM-875180305/p/9672234.html