私信  •  关注

ewwink

ewwink 最近创建的主题
ewwink 最近回复了
7 年前
回复了 ewwink 创建的主题 » python selenium-get(ctrl-u)等价的页面源代码

如果我没有错,你想比较原始的html ctrl+U 和呈现的html browser.page_source ,您可以使用 requests

import requests

originalHTML = requests.get('http://...').text
print(originalHTML)

或者可以为 view-source:

url = 'https://..../'
browser.get(url)
renderedHTML = browser.page_source

# open blank page because JS cannot open special URL like `view-source:`
browser.execute_script("window.open('about:blank', '_blank')")
# switch to tab 2
browser.switch_to_window(browser.window_handles[1])
browser.get("view-source:" + url)
originalHTML = originalHTML = browser.find_element_by_css_selector('body').text

# switch to tab 1
#browser.switch_to_window(browser.window_handles[0])

你可以试试这个

....
var continue_spinning = true;

....
var stopSpinning = function() {
    if (continue_spinning) {
      setTimeout(stopSpinning, my_timer);
      return
    }
    ....

$('#gen').click(function() {
  if (continue_spinning) {
    continue_spinning = false;
    $('#gen').html('Start');
  }
  else {
    $('.reel-container:first').slotMachine(randGen());
    continue_spinning = true;
    $('#gen').html('Stop');
  }
});

演示:

var reeling_time = 500;
var stop_spinning_time_difference = 350;
var start_spinning_time = 0;
var currency_symbol = "$";
var continue_spinning = true;

$.fn.slotMachine = function(my_number) {

  var $parentSlot = this;
  var hidden_reels_html = '';
  var hidden_reels_array = [];
  var numberFormat = function number_format(number) {
    number = (number + '');
    return number;
  }

  for (var $j = 0; $j <= 9; $j++) {
    hidden_reels_array[$j] = "";
    for (var $i = 0; $i <= 9; $i++) {
      hidden_reels_array[$j] += '<div class="reel-symbol' + ($i == 0 ? ' reel-loop' : '') + '">' + (($j + $i) % 10) + '</div>';
    }
  }
  var transformNumberToArrayPlusDollar = function(my_number) {
    var my_scale = parseInt(my_number, 10) > 999 ? 0 : 2;

    my_number = numberFormat(my_number, my_scale, ".", ",");

    var my_number_array = my_number.split('');

    // my_number_array.unshift(currency_symbol);

    return my_number_array;
  };

  //Effect for the reel to go up and then down like it is pushed to spin
  var effectBeforeSpin = function() {
    $parentSlot.find('.main-reel-symbol').removeClass('reel-stop').addClass('reel-begin');
  };

  var slotMachine = function(my_number) {

    var my_number_array = transformNumberToArrayPlusDollar(my_number);
    var reels_html = '';

    for (var $i = 0; $i < my_number_array.length; $i++) {
      reels_html += '<div class="reel">' + hidden_reels_array[($i % 10)] + '</div>';
    }
    effectBeforeSpin();
    var startSpinning = function() {
      $parentSlot.html(reels_html);
      var my_timer = reeling_time;
      $.each(my_number_array, function(my_index, my_value) {
        var next_value = /^[0-9]$/.test(my_value) ? (parseInt(my_value, 10) + 1) % 10 : "0";
        var stopSpinning = function() {
          if (continue_spinning) {
            setTimeout(stopSpinning, my_timer);
            return
          }
          $parentSlot.find('.reel:eq(' + my_index + ')').html("<div class='reel-symbol main-reel-symbol reel-stop'>" + my_value + "</div>")
            .append("<div class='reel-symbol'>" + next_value + "</div>");
        };


        setTimeout(stopSpinning, my_timer);

        my_timer += stop_spinning_time_difference;


      });

    };

    setTimeout(startSpinning, start_spinning_time);
  };

  slotMachine(my_number);

  return this;

};

function randGen() {
  var randNum = (Math.floor(Math.random() * 999) + 1).toString()
  if (randNum.toString().length == 3) {
    return randNum;
  }
  else if (randNum.toString().length == 2) {
    return "0" + randNum;
  }
  else if (randNum.toString().length == 1) {
    return "00" + randNum;
  }
}


$('.reel-container:first').slotMachine('00' + 1).toString();

if (continue_spinning) {
  $('#gen').html('Stop');
}
$('#gen').click(function() {
  if (continue_spinning) {
    continue_spinning = false;
    $('#gen').html('Start');
    //$('#gen').attr('disabled', true)
  }
  else {
    $('.reel-container:first').slotMachine(randGen());
    continue_spinning = true;
    $('#gen').html('Stop');
  }
});
@-moz-keyframes reel-loop {
  from {
    margin-top: 0px;
  }
  to {
    margin-top: -480px;
  }
}

@-webkit-keyframes reel-loop {
  from {
    margin-top: 0px;
  }
  to {
    margin-top: -480px;
  }
}

@keyframes reel-loop {
  from {
    margin-top: 0px;
  }
  to {
    margin-top: -480px;
  }
}

@-moz-keyframes reel-begin {
  0% {
    margin-top: 0px;
  }
  75% {
    margin-top: -60px;
  }
  100% {
    margin-top: 20px;
  }
}

@-webkit-keyframes reel-begin {
  0% {
    margin-top: 0px;
  }
  75% {
    margin-top: -60px;
  }
  100% {
    margin-top: 20px;
  }
}

@keyframes reel-begin {
  0% {
    margin-top: 0px;
  }
  75% {
    margin-top: -60px;
  }
  100% {
    margin-top: 20px;
  }
}

@-moz-keyframes reel-stop {
  from {
    top: -50px;
  }
  to {
    top: 0px;
  }
}

@-webkit-keyframes reel-stop {
  from {
    top: -50px;
  }
  to {
    top: 0px;
  }
}

@keyframes reel-stop {
  from {
    top: -50px;
  }
  to {
    top: 0px;
  }
}

.main-container {
  margin: 0 auto;
}

.reel {
  width: 70px;
  height: 90px;
  background: #DAC290;
  border: 4px solid #C7A365;
  float: left;
  margin-right: 10px;
}

.reel-symbol {
  color: black;
  font-size: 60px;
  line-height: 74px;
  height: 74px;
  vertical-align: middle;
  text-align: center;
  overflow: hidden;
}

.reel-container {
  height: 90px;
  overflow: hidden;
  position: relative;
}

.reel-loop {
  -moz-animation-duration: 0.5s;
  -webkit-animation-duration: 0.5s;
  animation-duration: 0.5s;
  -moz-animation-name: reel-loop;
  -webkit-animation-name: reel-loop;
  animation-name: reel-loop;
  -moz-animation-iteration-count: infinite;
  -webkit-animation-iteration-count: infinite;
  animation-iteration-count: infinite;
  -moz-animation-timing-function: linear;
  -webkit-animation-timing-function: linear;
  animation-timing-function: linear;
  -moz-animation-direction: reverse;
  -webkit-animation-direction: reverse;
  animation-direction: reverse;
}

.reel-stop {
  -moz-animation-duration: 0.15s;
  -webkit-animation-duration: 0.15s;
  animation-duration: 0.15s;
  -moz-animation-name: reel-stop;
  -webkit-animation-name: reel-stop;
  animation-name: reel-stop;
  -moz-animation-timing-function: ease-out;
  -webkit-animation-timing-function: ease-out;
  animation-timing-function: ease-out;
}

.reel-begin {
  -moz-animation-duration: 0.35s;
  -webkit-animation-duration: 0.35s;
  animation-duration: 0.35s;
  -moz-animation-name: reel-begin;
  -webkit-animation-name: reel-begin;
  animation-name: reel-begin;
  -moz-animation-timing-function: linear;
  -webkit-animation-timing-function: linear;
  animation-timing-function: linear;
}
<!DOCTYPE html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<link rel='stylesheet' href='http://cdnjs.cloudflare.com/ajax/libs/animate.css/3.2.3/animate.min.css'>
<!-- Range -->
<link rel='stylesheet' href='http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css'>
<div class="bg-img text-center">
  <div class='main-container'>
    <div class='reel-container'></div>
  </div>
  <button id="gen">Gen</button>
</div>
<script src='http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js'></script>
<script src="js/range.js"></script>
<script src='http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js'></script>
7 年前
回复了 ewwink 创建的主题 » python请求没有提取所有元素

HTML无效,已中断 BeautifulSoup 在这里修理

....
html_doc = response.text.replace('<table <', '<')
html_doc = re.sub(r'<\!--\s+\d+\s+--\!>', '', html_doc)
html_doc = re.sub(r'</?font.*?>' ,'', html_doc)
soup = BeautifulSoup(html_doc, "html.parser")

trs=  soup.findAll('table')[8].findAll('tr')
print (len(trs))

注:使用 lxml 返回7而不是22

7 年前
回复了 ewwink 创建的主题 » 需要了解如何使用python刮取实时流数据的帮助

main.html 是错误的url,它用于显示来自 temp_report.html (阿贾克斯)

import requests
from bs4 import BeatifulSoup
import time

def get_count():
    url = "http://10.0.0.206/temp_report.html
    # or
    # url = "http://10.0.0.206/apps/cy8ckit_062_demo/temp_report.html

    # request with fake header, otherwise you will get an 403 HTTP error
    r = request.get(url, headers={'User-Agent': Mozilla/5.0})
    page_source = r.text
    # print(page_source)

    soup = BeautifulSoup(page_source, 'html.parser')
    print(soup)

    # html_body = soup.find('body') # <body>this_text</body>
    # print(html_body.text) # this_text

    # paragraphs = soup.find_all('p') # <body> <p>p1</p> <p>p2</p> </body>
    # for p in paragraphs:
    #    print(p.text) # p1, p2


while True:
    print(get_count())
    time.sleep(8)