如何使用 Robobrowser 或 Request 提交 Web 表单
How To Submit A Web Form Using Robobrowser Or Request
我不是 Python 专家,但发现它在许多领域都非常有用。在提交表单和检索输出的上下文中,我在两个不同的场合开始使用 Robobrowser 和 Requests 时遇到了问题。我想做的事;使用网络检查器向 westjet.com 提交旅行日期 我看到 "origin-event" 和 "destination-event" 我需要在我的案例中提交机场代码 YYZ 和 POP 以及日期。有人可以帮助我理解这些库,以便我知道如何完成此任务并提供适当的解释,谢谢。
import requests
headers = {'User-Agent': 'Safari 9.0'}
print("Please Print Your Intended Departure Date")
#departure_date = input()
print("Please Print Your Intended Return Date")
#return_date = input()
#
# FLIGHTS ONLY
#
westjetF = "https://www.westjet.com/en-ca/index"
airtransatF = "http://www.airtransat.com/en-CA/home"
sunwingF = "http://www.sunwing.ca"
hotwireF = "https://www.hotwire.com"
#
# VACATIONS ONLY
#
westjetV = "https://www.westjet.com/en-ca/index"
airtransatV = "https://www.transat.com/en-CA/?ici=homepage&icn=moteur_forfait&_ga=1.139381168.282228778.1458947467&search=package&origin=YOW"
airtransatV = "http://www.airtransat.com/en-CA/home"
sunwingV = "http://www.sunwing.ca"
# Payload for Westjet.com yy/mm/dd 00-00-00
Flight_data = {
'orgin-event': 'POP',
'destination-event' : 'YYZ',
'depart' : '2017-04-22' ,
'return': '2017-04-24',
# 'numAdults' : '1',
# 'numChildren': '0',
#'numInfants': '0',
#'promoCode': ''
}
with requests.Session() as s:
execute = s.post(westjetF,data=Flight_data)
print(execute.text)
status = execute.status_code
if(status == 200):
print("great, Request was processed")
else:
print("Sorry That Request Wasnt Processed")
好的,这是 post 和 requests
的方法
import requests
from bs4 import BeautifulSoup ## for html parsing ##
url = 'http://www.westjet.com' ## the page you want to GET or POST ##
## Now get the input names ( with web inspector ) ##
## and make a dictionary with the inputs and data you wand to post ##
post_data = { 'name1' : 'value1', 'name2' : 'value2', etc }
req = requests.post(url, data=post_data) ## post your data to the page ##
# req = requests.get(url, params=post_data) ## use this method if you want to GET the page ##
status = req.status_code ## this is the HTTP response status, eg 200 ##
html = req.text ## this is the response content ( html ) ##
## Next , you can process your html with BeautifulSoup ##
## or you can skip this part if you dont care about it ##
soup = BeautifulSoup(html, "html.parser")
table = soup.find('table') ## This will find the first table , for example ##
当然这是一个非常基本的示例,您还可以做很多事情。
希望对大家有所帮助
我不是 Python 专家,但发现它在许多领域都非常有用。在提交表单和检索输出的上下文中,我在两个不同的场合开始使用 Robobrowser 和 Requests 时遇到了问题。我想做的事;使用网络检查器向 westjet.com 提交旅行日期 我看到 "origin-event" 和 "destination-event" 我需要在我的案例中提交机场代码 YYZ 和 POP 以及日期。有人可以帮助我理解这些库,以便我知道如何完成此任务并提供适当的解释,谢谢。
import requests
headers = {'User-Agent': 'Safari 9.0'}
print("Please Print Your Intended Departure Date")
#departure_date = input()
print("Please Print Your Intended Return Date")
#return_date = input()
#
# FLIGHTS ONLY
#
westjetF = "https://www.westjet.com/en-ca/index"
airtransatF = "http://www.airtransat.com/en-CA/home"
sunwingF = "http://www.sunwing.ca"
hotwireF = "https://www.hotwire.com"
#
# VACATIONS ONLY
#
westjetV = "https://www.westjet.com/en-ca/index"
airtransatV = "https://www.transat.com/en-CA/?ici=homepage&icn=moteur_forfait&_ga=1.139381168.282228778.1458947467&search=package&origin=YOW"
airtransatV = "http://www.airtransat.com/en-CA/home"
sunwingV = "http://www.sunwing.ca"
# Payload for Westjet.com yy/mm/dd 00-00-00
Flight_data = {
'orgin-event': 'POP',
'destination-event' : 'YYZ',
'depart' : '2017-04-22' ,
'return': '2017-04-24',
# 'numAdults' : '1',
# 'numChildren': '0',
#'numInfants': '0',
#'promoCode': ''
}
with requests.Session() as s:
execute = s.post(westjetF,data=Flight_data)
print(execute.text)
status = execute.status_code
if(status == 200):
print("great, Request was processed")
else:
print("Sorry That Request Wasnt Processed")
好的,这是 post 和 requests
import requests
from bs4 import BeautifulSoup ## for html parsing ##
url = 'http://www.westjet.com' ## the page you want to GET or POST ##
## Now get the input names ( with web inspector ) ##
## and make a dictionary with the inputs and data you wand to post ##
post_data = { 'name1' : 'value1', 'name2' : 'value2', etc }
req = requests.post(url, data=post_data) ## post your data to the page ##
# req = requests.get(url, params=post_data) ## use this method if you want to GET the page ##
status = req.status_code ## this is the HTTP response status, eg 200 ##
html = req.text ## this is the response content ( html ) ##
## Next , you can process your html with BeautifulSoup ##
## or you can skip this part if you dont care about it ##
soup = BeautifulSoup(html, "html.parser")
table = soup.find('table') ## This will find the first table , for example ##
当然这是一个非常基本的示例,您还可以做很多事情。
希望对大家有所帮助