Django error's when I try to view webpage source - python

I'm using Django to create a web app for a class. The snippet of HTML below is what I think might be causing my problem.
<form action="press" method="POST">
<table>
<tr>
<td colspan="4">
{% if output %}
<input id="output" name="output" type="text" value="{{output}}" readonly>
{% else %}
<input id="output_hidden" name="output" type="text" value="0" readonly>
{% endif %}
<input type="hidden" name="buttVal" value="{{buttVal}}">
<input type="hidden" name="currVal" value="{{currVal}}">
<input type="hidden" name="prevVal" value="{{prevVal}}">
<input type="hidden" name="math" value="{{math}}">
{% csrf_token %}
</td>
I'm using hidden fields to store values to keep the session stateless (point of the assignment). I'm trying to debug my hidden fields, however, after I start my app and trigger the first if statement below, I'm no longer able to view the webpage source (by right clicking in the webpage and clicking on "view source"). When I attempt this, it first says "Document Expired" but it allows me to try the request again. When I try it again, the html below shows up as source.
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="robots" content="NONE,NOARCHIVE">
<title>MultiValueDictKeyError at /press</title>
<style type="text/css">
html * { padding:0; margin:0; }
body * { padding:10px 20px; }
body * * { padding:0; }
body { font:small sans-serif; }
body>div { border-bottom:1px solid #ddd; }
h1 { font-weight:normal; }
h2 { margin-bottom:.8em; }
h2 span { font-size:80%; color:#666; font-weight:normal; }
h3 { margin:1em 0 .5em 0; }
h4 { margin:0 0 .5em 0; font-weight: normal; }
code, pre { font-size: 100%; white-space: pre-wrap; }
table { border:1px solid #ccc; border-collapse: collapse; width:100%; background:white; }
tbody td, tbody th { vertical-align:top; padding:2px 3px; }
thead th { padding:1px 6px 1px 3px; background:#fefefe; text-align:left; font-weight:normal; font-size:11px; border:1px solid #ddd; }
tbody th { width:12em; text-align:right; color:#666; padding-right:.5em; }
table.vars { margin:5px 0 2px 40px; }
table.vars td, table.req td { font-family:monospace; }
table td.code { width:100%; }
table td.code pre { overflow:hidden; }
table.source th { color:#666; }
table.source td { font-family:monospace; white-space:pre; border-bottom:1px solid #eee; }
ul.traceback { list-style-type:none; color: #222; }
ul.traceback li.frame { padding-bottom:1em; color:#666; }
ul.traceback li.user { background-color:#e0e0e0; color:#000 }
div.context { padding:10px 0; overflow:hidden; }
div.context ol { padding-left:30px; margin:0 10px; list-style-position: inside; }
div.context ol li { font-family:monospace; white-space:pre; color:#777; cursor:pointer; }
div.context ol li pre { display:inline; }
div.context ol.context-line li { color:#505050; background-color:#dfdfdf; }
div.context ol.context-line li span { position:absolute; right:32px; }
.user div.context ol.context-line li { background-color:#bbb; color:#000; }
.user div.context ol li { color:#666; }
div.commands { margin-left: 40px; }
div.commands a { color:#555; text-decoration:none; }
.user div.commands a { color: black; }
#summary { background: #ffc; }
#summary h2 { font-weight: normal; color: #666; }
#explanation { background:#eee; }
#template, #template-not-exist { background:#f6f6f6; }
#template-not-exist ul { margin: 0 0 0 20px; }
#unicode-hint { background:#eee; }
#traceback { background:#eee; }
#requestinfo { background:#f6f6f6; padding-left:120px; }
#summary table { border:none; background:transparent; }
#requestinfo h2, #requestinfo h3 { position:relative; margin-left:-100px; }
#requestinfo h3 { margin-bottom:-1em; }
.error { background: #ffc; }
.specific { color:#cc3300; font-weight:bold; }
h2 span.commands { font-size:.7em;}
span.commands a:link {color:#5E5694;}
pre.exception_value { font-family: sans-serif; color: #666; font-size: 1.5em; margin: 10px 0 10px 0; }
</style>
<script type="text/javascript">
//<!--
function getElementsByClassName(oElm, strTagName, strClassName){
// Written by Jonathan Snook, http://www.snook.ca/jon; Add-ons by Robert Nyman, http://www.robertnyman.com
var arrElements = (strTagName == "*" && document.all)? document.all :
oElm.getElementsByTagName(strTagName);
var arrReturnElements = new Array();
strClassName = strClassName.replace(/\-/g, "\-");
var oRegExp = new RegExp("(^|\s)" + strClassName + "(\s|$)");
var oElement;
for(var i=0; i<arrElements.length; i++){
oElement = arrElements[i];
if(oRegExp.test(oElement.className)){
arrReturnElements.push(oElement);
}
}
return (arrReturnElements)
}
function hideAll(elems) {
for (var e = 0; e < elems.length; e++) {
elems[e].style.display = 'none';
}
}
window.onload = function() {
hideAll(getElementsByClassName(document, 'table', 'vars'));
hideAll(getElementsByClassName(document, 'ol', 'pre-context'));
hideAll(getElementsByClassName(document, 'ol', 'post-context'));
hideAll(getElementsByClassName(document, 'div', 'pastebin'));
}
function toggle() {
for (var i = 0; i < arguments.length; i++) {
var e = document.getElementById(arguments[i]);
if (e) {
e.style.display = e.style.display == 'none' ? 'block': 'none';
}
}
return false;
}
function varToggle(link, id) {
toggle('v' + id);
var s = link.getElementsByTagName('span')[0];
var uarr = String.fromCharCode(0x25b6);
var darr = String.fromCharCode(0x25bc);
s.innerHTML = s.innerHTML == uarr ? darr : uarr;
return false;
}
function switchPastebinFriendly(link) {
s1 = "Switch to copy-and-paste view";
s2 = "Switch back to interactive view";
link.innerHTML = link.innerHTML == s1 ? s2: s1;
toggle('browserTraceback', 'pastebinTraceback');
return false;
}
//-->
Outside of this, I'm not really sure what's going on. I tried googling the problem, but my lack of understanding of this situation kept me from finding anything useful. Hopefully someone can point out something that I'm not doing correctly.
-- EDIT --
Here's my views.py file.
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home_page(request):
index = 'calculator/calculator.html'
return render(request,index,{})
def press(request):
context = {}
operators = ['plus','minus','divide','multiply']
index = 'calculator/calculator.html'
## Discerns from a number and an operator press
if 'val' in request.POST and is_int(request.POST['val']):
if request.POST['output'] == '0':
context['output'] = request.POST['val']
context['buttVal'] = request.POST['val']
context['currVal'] = request.POST['val']
elif request.POST['currVal'] == '0':
context['output'] = request.POST['val']
context['buttVal'] = request.POST['val']
context['currVal'] = request.POST['val']
else:
newOutput = request.POST['output'] + request.POST['val']
context['buttVal'] = request.POST['val']
context['currVal'] = newOutput
context['output'] = newOutput
context['math'] = request.POST['math']
context['prevVal'] = request.POST['prevVal']
elif request.POST['val'] == 'clear':
context['output'] = 0
context['buttVal'] = 0
context['currVal'] = 0
context['prevVal'] = 0
context['math'] = ''
elif request.POST['val'] in operators:
print request.POST
if request.POST['math'] != '':
print "poop"
if request.POST['math'] == 'plus':
result = int(request.POST['currVal']) + int(request.POST['prevVal'])
context['prevVal'] = result
context['output'] = result
if request.POST['math'] == 'minus':
result = int(request.POST['currVal']) - int(request.POST['prevVal'])
context['prevVal'] = result
context['output'] = result
if request.POST['math'] == 'multiply':
result = int(request.POST['currVal']) * int(request.POST['prevVal'])
context['prevVal'] = result
context['output'] = result
if request.POST['math'] == 'divide':
result = int(request.POST['currVal']) / int(request.POST['prevVal'])
context['prevVal'] = result
context['output'] = result
if request.POST['val'] == 'equal':
pass
else:
context['prevVal'] = request.POST['currVal']
#context['math'] = request.POST['val']
context['output'] = request.POST['output']
context['math'] = request.POST['val']
context['buttVal'] = request.POST['buttVal']
context['currVal'] = 0
else:
context['output'] = request.POST['output']
context['buttVal'] = request.POST['buttVal']
context['math'] = request.POST['math']
context['prevVal'] = request.POST['prevVal']
## Submits POST
return render(request, index, context)
## Helper functions
def is_int(string):
try:
int(string)
return True
except ValueError:
return False

Your are trying to access to a parameters that are not exist in the request.POST. For example:
elif request.POST['val'] == 'clear':
...
elif request.POST['val'] in operators:
...
else:
context['output'] = request.POST['output']
context['buttVal'] = request.POST['buttVal']
context['math'] = request.POST['math']
context['prevVal'] = request.POST['prevVal']
You didn't check if val, output, buttVal, prevVal and math are in the request.POST.
To safely get values from the request.POST use the get() method:
request.POST.get('val')
This expression will return the val parameter of the request.POST or None if such parameter doesn't exist.
Add the print request.POST and the very beginning of the press() view to see the content of the POST data in the "view source" request.

This happens, because when you view source of a page in a browser, another request is made; but its a GET request.
Thus, your POST dictionary doesn't have those keys, resulting in the exception.
The easiest way to solve this problem is to use forms.
class MyForm(forms.Form):
output = forms.TextField()
# .. your other fields here
def press(request):
form = MyForm(request.POST or None)
if form.is_valid():
# do stuff
return redirect('/home')
return render(request, 'template.html', {'form': form})
You can read more about forms (including, how to hide fields and pass initial values) at the documentation.

Related

Need suggestions : Django UserProfile geographic address to connect with others users

I hope you're well,
I'm looking for a plugin or a tutorial for :
a- allow my users to fill in their address on their profile. The best would be to have a map on which they can identify their address or like on the UberEats site.
b- to be able to find the closest users according to their address.
If you have any ideas, I'm interested,
I have already made the profile on Django, all I need is the address field.
The below code will do. You will need Javascript to play around a bit and set your form fields with the returned address.
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
.controls {
margin-top: 10px;
border: 1px solid transparent;
border-radius: 2px 0 0 2px;
box-sizing: border-box;
-moz-box-sizing: border-box;
height: 32px;
outline: none;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}
#pac-input {
background-color: #fff;
font-family: Roboto;
font-size: 15px;
font-weight: 300;
margin-left: 12px;
padding: 0 11px 0 13px;
text-overflow: ellipsis;
width: 300px;
}
#pac-input:focus {
border-color: #4d90fe;
}
.pac-container {
font-family: Roboto;
}
#type-selector {
color: #fff;
background-color: #4d90fe;
padding: 5px 11px 0px 11px;
}
#type-selector label {
font-family: Roboto;
font-size: 13px;
font-weight: 300;
}
</style>
<title>Places Searchbox</title>
<style>
#target {
width: 345px;
}
</style>
</head>
<body>
<input id="pac-input" class="controls" type="text" placeholder="Search Box">
<div id="map"></div>
<script>
// This example adds a search box to a map, using the Google Place Autocomplete
// feature. People can enter geographical searches. The search box will return a
// pick list containing a mix of places and predicted search terms.
function initAutocomplete() {
var map = new google.maps.Map(document.getElementById('map'), {
center: { lat: -33.8688, lng: 151.2195 },
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
// Create the search box and link it to the UI element.
var input = document.getElementById('pac-input');
var searchBox = new google.maps.places.SearchBox(input);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
// Bias the SearchBox results towards current map's viewport.
map.addListener('bounds_changed', function () {
searchBox.setBounds(map.getBounds());
});
map.addListener('click', function (locationdata) {
console.log(locationdata.latLng.lat());
console.log(locationdata.latLng.lng());
var myLatlng = new google.maps.LatLng(locationdata.latLng.lat(), locationdata.latLng.lng());
var myOptions = {
zoom: 13,
center: myLatlng
}
});
var geocoder = new google.maps.Geocoder();
google.maps.event.addListener(map, 'click', function (event) {
geocoder.geocode({
'latLng': event.latLng
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
alert(results[0].formatted_address);
}
}
});
});
var markers = [];
// [START region_getplaces]
// Listen for the event fired when the user selects a prediction and retrieve
// more details for that place.
searchBox.addListener('places_changed', function () {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
// Clear out the old markers.
markers.forEach(function (marker) {
marker.setMap(null);
});
markers = [];
// For each place, get the icon, name and location.
var bounds = new google.maps.LatLngBounds();
places.forEach(function (place) {
console.log("lat: " + place.geometry.location.lat() + " lng: " + place.geometry.location.lng())
var icon = {
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25)
};
// Create a marker for each place.
markers.push(new google.maps.Marker({
map: map,
icon: icon,
title: place.name,
position: place.geometry.location
}));
if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
});
// [END region_getplaces]
}
</script>
<script
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAkwr5rmzY9btU08sQlU9N0qfmo8YmE91Y&libraries=places&callback=initAutocomplete"
async defer></script>
</body>
</html>
You will need to add markers on map for each select. The code will console log the long and lat also, and return an alert with the address once you click it.
Credit goes to these two threads Link1 and Link2.
I managed to combine both codes and give results as you have mentioned.

Interacting with ChartJS from Flask

I am getting really confused on what to do here. I have a python script that does some cool things and then should display those cool things via some charts.
The way I've seen it's done is via Flask and ChartJS, but every website that I've seen uses it to display static content, which, of course, is not what I need.
I've tried doing this with this template:
<!doctype html>
<html>
<head>
<title>Line Chart</title>
<script src="/static/chartjs/Chart.min.js"></script>
<script src="/static/chartjs/utils.js"></script>
<style>
canvas{
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
</style>
</head>
<body>
<div style="width:75%;">
<canvas id="canvas"></canvas>
</div>
<br>
<br>
<button id="randomizeData">Randomize Data</button>
<button id="addDataset">Add Dataset</button>
<button id="removeDataset">Remove Dataset</button>
<button id="addData">Add Data</button>
<button id="removeData">Remove Data</button>
<script>
var MONTHS = [];
var config = {
type: 'line',
data: {
labels: ['1/05/19\n00:57', '1/05/19\n00:58', '1/05/19\n00:59', '1/05/19\n01:00', '1/05/19\n01:01', '1/05/19\n01:02', '1/05/19\n01:03'],
{{ dataset }}
},
options: {
responsive: true,
title: {
display: true,
text: 'Chart.js Line Chart'
},
tooltips: {
mode: 'index',
intersect: false,
},
hover: {
mode: 'nearest',
intersect: true
},
scales: {
xAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'Month'
}
}],
yAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'Value'
}
}]
}
}
};
window.onload = function() {
var ctx = document.getElementById('canvas').getContext('2d');
window.myLine = new Chart(ctx, config);
};
document.getElementById('randomizeData').addEventListener('click', function() {
config.data.datasets.forEach(function(dataset) {
dataset.data = dataset.data.map(function() {
return randomScalingFactor();
});
});
window.myLine.update();
});
var colorNames = Object.keys(window.chartColors);
document.getElementById('addDataset').addEventListener('click', function() {
var colorName = colorNames[config.data.datasets.length % colorNames.length];
var newColor = window.chartColors[colorName];
var newDataset = {
label: 'Dataset ' + config.data.datasets.length,
backgroundColor: newColor,
borderColor: newColor,
data: [],
fill: false
};
for (var index = 0; index < config.data.labels.length; ++index) {
newDataset.data.push(randomScalingFactor());
}
config.data.datasets.push(newDataset);
window.myLine.update();
});
document.getElementById('addData').addEventListener('click', function() {
if (config.data.datasets.length > 0) {
var month = MONTHS[1];
config.data.labels.push(month);
config.data.datasets.forEach(function(dataset) {
dataset.data.push(randomScalingFactor());
});
window.myLine.update();
}
});
document.getElementById('removeDataset').addEventListener('click', function() {
config.data.datasets.splice(0, 1);
window.myLine.update();
});
document.getElementById('removeData').addEventListener('click', function() {
config.data.labels.splice(-1, 1); // remove the label first
config.data.datasets.forEach(function(dataset) {
dataset.data.pop();
});
window.myLine.update();
});
</script>
</body>
</html>
This contains a lot of unused stuff (it was downloaded directly from the chart.js samples) and slightly altered by me (see {{ dataset }})
And this is the python code I use to generate the dataset part:
def generate_dataset(self):
data = 'datasets: ['
for post_list_index, post in enumerate(self.post_list):
data = data + "{label: '" + post + "', backgroundColor: window.chartColors.red, borderColor: window.chartColors.red, data: ["
for index, history in enumerate(self.submissions_score_history):
score = history[post_list_index]
if index < len(self.submissions_score_history) - 1:
data = data + str(score) + ','
else:
data = data + str(score)
data = data + '],fill: false,}'
if post_list_index < len(self.post_list) - 1:
data = data + ','
else:
pass
data = data + ']'
return data
Now, unfortunately, this doesnt render anything. I'm pretty sure that this is due to the label: 'label'; being escaped to &#39label'.
I've searched about this issue and apparently this javascript injection thing I'm attempting to do has been removed because it represents a security vulnerability.
This project is quite literally just a dashboard for another small python project that will be used by me only, so I was trying to build something quickly and easily, but this is turning out to be quite troublesome :/

requests avoid ddos protection [duplicate]

This question already has an answer here:
A specific site is returning a different response on python and in chrome
(1 answer)
Closed 4 years ago.
When I am trying to access this link from: browser
I have that reponse:
{"success":false}
But when I'm trying to call it from lib requests, I use that code:
import requests
s = requests.Session()
h = s.get('https://csgopolygon.com/scripts/_createAccount.php')
print(h.text)
When I'm running code response is:
<html>
<head>
<meta charset="UTF-8">
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<style>
html, body {
width: 100%;
border: 0px;
padding: 0px;
margin: 0px;
font-size: 18px;
}
.content {
width: 800px;
max-width: 95%;
margin: 0 auto;
opacity: 0;
transition: all 0.4s ease 2s;
}
.content.show {
opacity: 1;
}
.form {
position: relative;
}
.form > .lefCol {
display: inline-block;
width: 48%;
min-width: 300px;
padding: 0px;
vertical-align: top;
}
.form > .rightCol {
display: inline-block;
width: 48%;
min-width: 300px;
padding: 0px;
vertical-align: top;
}
.button {
font-size: 32px;
padding: 5px;
}
.footer {
padding: 15px;
text-align: right;
}
#loading {
text-align: center;
}
#fountainTextG{
width:420px;
margin:auto;
}
.fountainTextG{
color:rgb(0,0,0);
font-family:Arial;
font-size:44px;
text-decoration:none;
font-weight:normal;
font-style:normal;
float:left;
animation-name:bounce_fountainTextG;
-o-animation-name:bounce_fountainTextG;
-ms-animation-name:bounce_fountainTextG;
-webkit-animation-name:bounce_fountainTextG;
-moz-animation-name:bounce_fountainTextG;
animation-duration:2.09s;
-o-animation-duration:2.09s;
-ms-animation-duration:2.09s;
-webkit-animation-duration:2.09s;
-moz-animation-duration:2.09s;
animation-iteration-count:infinite;
-o-animation-iteration-count:infinite;
-ms-animation-iteration-count:infinite;
-webkit-animation-iteration-count:infinite;
-moz-animation-iteration-count:infinite;
animation-direction:normal;
-o-animation-direction:normal;
-ms-animation-direction:normal;
-webkit-animation-direction:normal;
-moz-animation-direction:normal;
transform:scale(.5);
-o-transform:scale(.5);
-ms-transform:scale(.5);
-webkit-transform:scale(.5);
-moz-transform:scale(.5);
}#fountainTextG_1{
animation-delay:0.75s;
-o-animation-delay:0.75s;
-ms-animation-delay:0.75s;
-webkit-animation-delay:0.75s;
-moz-animation-delay:0.75s;
}
#fountainTextG_2{
animation-delay:0.9s;
-o-animation-delay:0.9s;
-ms-animation-delay:0.9s;
-webkit-animation-delay:0.9s;
-moz-animation-delay:0.9s;
}
#fountainTextG_3{
animation-delay:1.05s;
-o-animation-delay:1.05s;
-ms-animation-delay:1.05s;
-webkit-animation-delay:1.05s;
-moz-animation-delay:1.05s;
}
#fountainTextG_4{
animation-delay:1.2s;
-o-animation-delay:1.2s;
-ms-animation-delay:1.2s;
-webkit-animation-delay:1.2s;
-moz-animation-delay:1.2s;
}
#fountainTextG_5{
animation-delay:1.35s;
-o-animation-delay:1.35s;
-ms-animation-delay:1.35s;
-webkit-animation-delay:1.35s;
-moz-animation-delay:1.35s;
}
#fountainTextG_6{
animation-delay:1.5s;
-o-animation-delay:1.5s;
-ms-animation-delay:1.5s;
-webkit-animation-delay:1.5s;
-moz-animation-delay:1.5s;
}
#fountainTextG_7{
animation-delay:1.64s;
-o-animation-delay:1.64s;
-ms-animation-delay:1.64s;
-webkit-animation-delay:1.64s;
-moz-animation-delay:1.64s;
}
#fountainTextG_8{
animation-delay:1.79s;
-o-animation-delay:1.79s;
-ms-animation-delay:1.79s;
-webkit-animation-delay:1.79s;
-moz-animation-delay:1.79s;
}
#fountainTextG_9{
animation-delay:1.94s;
-o-animation-delay:1.94s;
-ms-animation-delay:1.94s;
-webkit-animation-delay:1.94s;
-moz-animation-delay:1.94s;
}
#fountainTextG_10{
animation-delay:2.09s;
-o-animation-delay:2.09s;
-ms-animation-delay:2.09s;
-webkit-animation-delay:2.09s;
-moz-animation-delay:2.09s;
}
#keyframes bounce_fountainTextG{
0%{
transform:scale(1);
color:rgb(0,0,0);
}
100%{
transform:scale(.5);
color:rgb(255,255,255);
}
}
#-o-keyframes bounce_fountainTextG{
0%{
-o-transform:scale(1);
color:rgb(0,0,0);
}
100%{
-o-transform:scale(.5);
color:rgb(255,255,255);
}
}
#-ms-keyframes bounce_fountainTextG{
0%{
-ms-transform:scale(1);
color:rgb(0,0,0);
}
100%{
-ms-transform:scale(.5);
color:rgb(255,255,255);
}
}
#-webkit-keyframes bounce_fountainTextG{
0%{
-webkit-transform:scale(1);
color:rgb(0,0,0);
}
100%{
-webkit-transform:scale(.5);
color:rgb(255,255,255);
}
}
#-moz-keyframes bounce_fountainTextG{
0%{
-moz-transform:scale(1);
color:rgb(0,0,0);
}
100%{
-moz-transform:scale(.5);
color:rgb(255,255,255);
}
}
</style>
</head>
<body>
<div class="content" id="captcha">
<h1>Stormwall DDoS protection</h1>
<form class="form" action="" method="POST">
<div class="lefCol">
<input type="hidden" name="swp_sessionKey" value="${sessionKey}">
<br/>
<div class="g-recaptcha" data-sitekey="${recaptchaPublicKey}"></div>
<br/>
<input type="submit" class="button" value="Go to website">
</div>
<div class="rightCol">
<p>Please confirm you are not a robot.</p>
<p>Пожалуйста, подтвердите, что Вы не робот.</p>
</div>
</form>
<div class="footer">stormwall.pro</div>
</div>
<div class="content" id="loading">
<div id="fountainTextG"><div id="fountainTextG_1" class="fountainTextG">L</div><div id="fountainTextG_2" class="fountainTextG">o</div><div id="fountainTextG_3" class="fountainTextG">a</div><div id="fountainTextG_4" class="fountainTextG">d</div><div id="fountainTextG_5" class="fountainTextG">i</div><div id="fountainTextG_6" class="fountainTextG">n</div><div id="fountainTextG_7" class="fountainTextG">g</div><div id="fountainTextG_8" class="fountainTextG">.</div><div id="fountainTextG_9" class="fountainTextG">.</div><div id="fountainTextG_10" class="fountainTextG">.</div></div>
</div>
<iframe id="reportFrame" title="Report frame" type="text/html" style="width: 1px; height: 1px; display: none;">
<h1>Automatic report form</h1>
<form id="reportForm" action="http://reports.stormwall.pro" method="post">
<input type="text" name="userAgent" value="python-requests/2.19.1">
<input type="text" name="ipAddress" value="195.218.182.93">
<input type="text" name="host" value="csgopolygon.com">
</form>
</iframe>
<script type="text/javascript">
var timer1 = setTimeout(function(){
document.getElementById("captcha").className = "content show";
}, 10);
const cE = "386t98trrw9mjgo1gcd4jvkk9vae?!yt7u1031qg64h3rywryo5dahsgfszhg:ka8crny2n4o26v";
const cK = 2;
const cN = "swp_token";
const cO = ";path=/;max-age=1800";
try {
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
let avd = require('./avd');
let abc = '0123456789qwertyuiopasdfghjklzxcvbnm:?!';
let _rpct = [];
let _rRpct = {};
function gTb(abc) {
for(let idx=0; idx<abc.length; idx++ ) {
_rpct[idx] = abc[idx];
_rRpct[abc[idx]] = idx;
}
}
function csr(key, text) {
let mIn = _rpct.length - 1;
let rs = '';
for(let idx=0; idx<text.length; idx++) {
let sbl = text[idx];
if( typeof(_rRpct[sbl]) == 'undefined' ) {
rs = rs + sbl;
} else {
let nCD = _rRpct[sbl] + key;
if( nCD > mIn ) {
nCD = nCD - mIn - 1;
} else if( nCD < 0 ) {
nCD = mIn + nCD + 1;
}
rs = rs + _rpct[nCD];
}
}
return rs;
}
function vgE(sk, text) {
let mIn = _rpct.length - 1;
let crK = sk;
let rs = '';
for(let idx=0; idx<text.length; idx++) {
let sbr = '' + text[idx];
rs = rs + csr(crK, sbr);
crK = crK + 1;
if( crK > mIn ) {
crK = 0;
}
}
return rs;
}
function vgD(sk, text) {
let mIn = _rpct.length - 1;
let crK = sk;
let rs = '';
if( avd() )
rs += Date.new().toString();
for(let idx=0; idx<text.length; idx++) {
let sbr = '' + text[idx];
rs = rs + csr(crK * -1, sbr);
crK = crK + 1;
if( crK > mIn ) {
crK = 0;
}
}
return rs;
}
gTb(abc);
let dCC = vgD(cK, cE);
document.cookie = cN + '=' + dCC + cO;
window.location = window.location;
clearTimeout(timer1);
let captcha = document.getElementById('captcha');
captcha.parentNode.removeChild(captcha);
document.getElementById('loading').className = 'content show';
},{"./avd":2}],2:[function(require,module,exports){
var bcbs = {
a: window.callPhantom,
b: window._phantom,
c: window.__phantoma,
d: window.Buffer,
e: window.emit,
f: window.spawn,
g: window.webdriver,
h: window.domAutomation
};
function vd() {
for(var i in bcbs) {
if( typeof(bcbs[i]) != 'undefined' ) {
return true;
}
}
return false;
}
module.exports = vd;
},{}]},{},[1]);
} catch(err) {
var iframe = '<form id="reportForm" action="http://reports.stormwall.pro" method="POST"><input type="text" name="userAgent" value="${userAgent}"><input type="text" name="ipAddress" value="${ipAddress}"><input type="text" name="host" value="${host}"><input type="text" name="jsError" value="${jsError}"></form>';
iframe = iframe + '<' + 'script type="text/javascript"' + '>document.getElementById("reportForm").submit();<' +'/script>';
var doc = document.getElementById('reportFrame').contentWindow.document;
doc.open();
doc.write( iframe.replace('${jsError}', err) );
doc.close();
}
</script>
</body>
</html>
It's ddos protection. How to avoid it? Or how to send captcha ? Thanks for any help.
I should have used some the headers from my browser
import requests
s = requests.Session()
s.headers = {
'cookie': 'swp_token=1529318441:da348e5038f36f4e22e839d6e317852a:c8fe351689c07b18b38cf1bb7e6604ff',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36'
}
h = s.get('https://csgopolygon.com/scripts/_createAccount.php')
print(h.text)

how to scrape an aspx rendered page using python

I am scraping an aspx rendered web page Link to Page
the website is .aspx, i chose Selenium , mechanize , urllib , lxml , Beautiful soup , requests . any insights/recommendations on coding the next steps. Also used scrapy.
I have used requests :
import requests
from bs4 import BeautifulSoup
request.get(url_to_page)
print request.content
it gives
<!DOCTYPE html>
<html>
<head><meta charset="utf-8" /><title>
www.tournamentsoftware.com
</title>
<style>
body {
font: normal 12px/20px Arial, Helvetica, sans-serif;
color: #505050;
background: #ccc url(//static.tournamentsoftware.com/images/cw_bg.png) repeat-x;
}
h2 {
font: bold 16px/16px Arial, Helvetica, sans-serif !important;
color: #000;
margin: 4px 0;
}
h4 {
font: bold 13px/13px Arial, Helvetica, sans-serif !important;
margin: 0 0 -8px 0;
}
p {
font: normal 12px/20px Arial, Helvetica, sans-serif;
margin: 12px 0;
}
p.note {
font: normal 10px/10px Arial, Helvetica, sans-serif;
margin: 8px 0 0 0;
text-align: center;
color: #999;
}
p.note.error {
font: bold 13px/20px Arial, Helvetica, sans-serif;
color: #f00;
}
.langtoggle { display:inline; margin-right:6px; }
.langtoggle.active { display:none; }
.langmessage { display:none; margin-bottom:20px; }
.langmessage.active { display:block; }
input.button {
margin: 4px 0;
}
</style>
</head>
<body>
<form method="post" action="./default.aspx?returnurl=%2fsport%2fdraw.aspx%3fid%3dE880C7A5-0A60-4A98-8FF9-A3B7DD58F3E2%26draw%3d4" id="form1" class="lang1033">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="p4eGoAC3005ctvGuhkv1w6Nanrs87p7iDcl4Hlk1SNw/cJovTDsJZeq54VdP4JR0injIJb59okjgeTpi30pz0LH9qjU=" />
<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="A86F2231" />
<div id="messagewindow">
<p class="toggles"><a id="Lang1033" class="lang langtoggle active" href="#" onclick="switchLang(this)">English</a> </p><div id="divLang1033" class="langmessage active"><h2>The use of cookies on www.tournamentsoftware.com</h2><p>We are legally obliged to get your
elems = document.getElementsByClassName('langmessage');
for (var i = 0; i < elems.length; i++) {
elems[i].className = 'langmessage';
}
document.getElementById(AThis.id).className = 'langtoggle active';
document.getElementById('div' + AThis.id).className = 'langmessage active';
return false;
}
function toggleCookiesHelp(AElmID) {
document.getElementById(AElmID).style.display = 'block';
return false;
}
function toggleCookiesHelpByClassName() {
var elems = document.getElementsByClassName('removecookies');
for (var i = 0; i < elems.length; i++) {
elems[i].style.display = 'block';
}
elems = document.getElementsByClassName('note');
for (var i = 0; i < elems.length; i++) {
elems[i].className = 'note error';
}
return false;
}
if (storageAvailable()) {
if (localStorage.getItem('cookiewall')) {
toggleCookiesHelpByClassName();
}
var elems = document.getElementsByClassName('button');
for (var i = 0; i < elems.length; i++) {
elems[i].addEventListener('click', function (e) {
localStorage.setItem('cookiewall', '1');
});
}
}
function storageAvailable() {
try {
var x = '__storage_test__';
localStorage.setItem(x, x);
localStorage.removeItem(x);
return true;
} catch(e) {
return false;
}
}
</script>
</form>
</body>
</html>
Also tried with mechanize, scrapy. All of them are giving this result only. How to scrape those websites. But I am able to see the source code in the browser. Is there any way to scrape those data.
import requests
from bs4 import BeautifulSoup
r_obj = requests.Session()
url = "http://www.tournamentsoftware.com/cookie/default.aspx?returnurl=%2fdefault.aspx"
fr_soup = r_obj.get(url)
soup = BeautifulSoup(fr_soup.content , "lxml")
#print soup
l = soup.find_all("input",type="hidden")
#print l
data = {
l[0]['name']:l[0]['value'],
l[1]['name']:l[1]['value'],
'btnAccept':'Yes, I accept'}
r_obj.post(url,verify=False,data=data)
url_needed = "http://www.tournamentsoftware.com/sport/draw.aspx?id=E880C7A5-0A60-4A98-8FF9-A3B7DD58F3E2&draw=4"
final = r_obj.get(url_needed)
#print final.content
soup1 = BeautifulSoup(final.content,"lxml")
detail_tab = soup1.find_all("table")
You need to use a framework which runs the client-side code for you. headless-chrome is one such tool.

Google Maps API to show highway exit/mile market in xml/json output?

I've developed a script for work that measures the distance traveled in a route by a vehicle, what's the longest distance traveled, and whether the route was from point A to B, a loop, or the vehicle was simply in idle.
I'm using GoogleMaps API to determine all of this, and search through the XML data to determine the current location of the vehicle. There's a 'Vicinity' tag that tells what the current city is at that long/lat coordinate.
I don't see anything describing the current highway exit or mile marker, does anyone know how to get this information through Google?
I apologize if this was asked before. I found a similar post but it was from 7 years ago and stated that GoogleMaps currently did not offer that sort of service.
I think it works pretty well.
It makes double markers sometimes (like two merge markers a few yards apart).
There is some extra code that's probably not used, and some things commented out.
I copy/pasted most of this from another project I was working on.
<style>
body {
width: 100%;
}
#right-panel, #map {
height: 400px;
width: 50%;
float: left;
overflow: auto;
}
hr {
clear: both;
}
</style>
<input id="from" placeholder="from" value="Tulsa"/>
<input id="to" placeholder="to" value="Chicago"/>
<select onchange="routeType(this)">
<option value="DRIVING">DRIVING</option>
<option value="WALKING">WALKING</option>
<option value="BICYCLING">BICYCLING</option>
<option value="TRANSIT">TRANSIT</option>
</select>
<input value="plot" onclick="calculateRoute()" type="button"/>
<hr/>
<div id="map"></div>
<div id="right-panel"></div>
<hr/>
<input onclick="calculateRoute()" value="GO" type="button"/>
<div id="maplog"></div>
<script type="text/javascript" src="https://maps.google.com/maps/api/js?libraries=geometry"></script>
<script>
var map;
var here = {};
var dest = {};
var directionsService;
var directionsDisplay;
var travelMode = 'DRIVING'; //'WALKING';
var sourcePosition;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 35, lng: -100},
zoom: 5,
mapTypeId: 'terrain'
});
var marker = new google.maps.Marker({
position: {lat: 50.8735506, lng: 4.3238525},
map: map,
title: 'Fermenthings winkel'
});
dest.marker = marker;
directionsService = new google.maps.DirectionsService;
directionsDisplay = new google.maps.DirectionsRenderer({
draggable: true,
suppressMarkers: true,
panel: document.getElementById('right-panel'),
map: map
});
}
function trackLocation() {
getLocation();
}
google.maps.event.addDomListener(window, 'load', initMap);
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
//x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
// x.innerHTML = "Latitude: " + position.coords.latitude +
// "<br>Longitude: " + position.coords.longitude;
if(map) {
sourcePosition = {lat: position.coords.latitude, lng: position.coords.longitude};
//map.setCenter(sourcePosition);
if(here.marker) {
here.marker.setMap(null);
}
here.marker = new google.maps.Marker({
position: sourcePosition,
draggable: true,
icon: {url: 'thumbnails/here.png'},
map: map
});
var bounds = new google.maps.LatLngBounds();
bounds.extend(sourcePosition);
bounds.extend(dest.marker.getPosition());
map.fitBounds(bounds);
google.maps.event.addListener(here.marker, 'position_changed', function() {
sourcePosition = {lat: this.getPosition().lat(), lng: this.getPosition().lng()};
});
google.maps.event.addListener(here.marker, 'dragend', function() {
sourcePosition = {lat: this.getPosition().lat(), lng: this.getPosition().lng()};
calculateRoute();
});
//return;
}
// route
calculateRoute();
}
function createMarker(lat, lng, title) {
return new google.maps.Marker({
position: {lat: lat, lng: lng},
map: map,
title: title
});
}
function displayRoute(origin, destination, service, display, waypoints, callback) {
if(waypoints) {
var options = {
origin: origin,
destination: destination,
waypoints: waypoints,
travelMode: travelMode //'DRIVING',
};
}
else {
var options = {
origin: origin,
destination: destination,
travelMode: travelMode //'DRIVING',
};
}
service.route(options, function(response, status) {
if (status === 'OK') {
//
//createMarkers(response, callback);
display.setDirections(response);
if(callback) {
callback(response);
}
} else {
// alert('Could not display directions due to: ' + status);
}
});
}
function routeType(elm) {
travelMode = elm.value;
if(sourcePosition) {
calculateRoute();
}
}
function calculateRoute() {
var from = document.getElementById('from').value;
var to = document.getElementById('to').value;
displayRoute(
from,
to,
directionsService,
directionsDisplay,
null, //waypoints,
function(response) {
// loop steps
var steps = response.routes[0].legs[0].steps;
for(var i = 0; i<steps.length; i++) {
var step = response.routes[0].legs[0].steps[i];
// search for keywords in the instructions
if( (step.instructions.indexOf('Take') > -1 || step.instructions.indexOf('take') > -1) && step.instructions.indexOf('exit') > -1) {
//here we enter a highway
document.getElementById('maplog').innerHTML += 'exit: ' + step.start_location.lat() +','+ step.start_location.lng() +'<br/>';
createMarker(step.start_location.lat(), step.start_location.lng(), 'exit');
}
if( (step.instructions.indexOf('Merge') > -1 || step.instructions.indexOf('merge') > -1) && step.instructions.indexOf('onto') > -1) {
//here we exit a highway
document.getElementById('maplog').innerHTML += 'merge: ' + step.start_location.lat() +','+ step.start_location.lng() +'<br/>';
createMarker(step.start_location.lat(), step.start_location.lng(), 'merge');
}
}
//document.getElementById('maplog').innerHTML = JSON.stringify(response);
}
);
}
</script>

Categories

Resources