国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - 在Python中使用CasperJS獲取JS渲染生成的HTML內容的教程

在Python中使用CasperJS獲取JS渲染生成的HTML內容的教程

2020-06-02 10:01Ihavegotyou Python

這篇文章主要介紹了在Python中使用CasperJS獲取JS渲染生成的HTML內容的教程,需要先用JavaScript創建一個接口文件,需要的朋友可以參考下

文章摘要:其實這里casperjs與python沒有直接關系,主要依賴casperjs調用phantomjs webkit獲取html文件內容。長期以來,爬蟲抓取 客戶端javascript渲染生成的html頁面 都極為 困難, Java里面有 HtmlUnit, 而Python里,我們可以使用獨立的跨平臺的CasperJS。

    創建site.js(接口文件,輸入:url,輸出:html file)  

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//USAGE: E:\toolkit\n1k0-casperjs-e3a77d0\bin>python casperjs site.js --url=http://spys.ru/free-proxy-list/IE/ --outputfile='temp.html'
  
 var fs = require('fs');
 var casper = require('casper').create({
  pageSettings: {
  loadImages: false,    
  loadPlugins: false,   
  userAgent: 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36 LBBROWSER'
 },
 logLevel: "debug",//日志等級
 verbose: true  // 記錄日志到控制臺
  });
 var url = casper.cli.raw.get('url');
 var outputfile = casper.cli.raw.get('outputfile');
 //請求頁面
 casper.start(url, function () {
 fs.write(outputfile, this.getHTML(), 'w');
 });
  
 casper.run();

    python 代碼, checkout_proxy.py      

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import json
   import sys
   #import requests
   #import requests.utils, pickle
   from bs4 import BeautifulSoup
   import os.path,os
   import threading
   #from multiprocessing import Process, Manager
   from datetime import datetime
   import traceback
   import logging
   import re,random
   import subprocess
   import shutil
   import platform
     
    
    
    
   output_file = os.path.join(os.path.dirname(os.path.realpath(__file__)),'proxy.txt')
   global_log = 'http_proxy' + datetime.now().strftime('%Y-%m-%d') + '.log'
   if not os.path.exists(os.path.join(os.path.dirname(os.path.realpath(__file__)),'logs')):
     os.mkdir(os.path.join(os.path.dirname(os.path.realpath(__file__)),'logs'))
   global_log = os.path.join(os.path.dirname(os.path.realpath(__file__)),'logs',global_log)
    
   logging.basicConfig(level=logging.DEBUG,format='[%(asctime)s] [%(levelname)s] [%(module)s] [%(funcName)s] [%(lineno)d] %(message)s',filename=global_log,filemode='a')
   log = logging.getLogger(__name__) 
   #manager = Manager()
   #PROXY_LIST = manager.list()
   mutex = threading.Lock()
   PROXY_LIST = []
    
    
   def isWindows():
     if "Windows" in str(platform.uname()):
     return True
     else:
     return False
    
    
   def getTagsByAttrs(tagName,pageContent,attrName,attrRegValue):
     soup = BeautifulSoup(pageContent)                                                
     return soup.find_all(tagName, { attrName : re.compile(attrRegValue) })
    
    
   def getTagsByAttrsExt(tagName,filename,attrName,attrRegValue):
     if os.path.isfile(filename):
     f = open(filename,'r')   
     soup = BeautifulSoup(f)
     f.close()
     return soup.find_all(tagName, { attrName : re.compile(attrRegValue) })
     else:
     return None
    
    
   class Site1Thread(threading.Thread):
     def __init__(self,outputFilePath):
       threading.Thread.__init__(self)
     self.outputFilePath = outputFilePath
     self.fileName = str(random.randint(100,1000)) + ".html"
     self.setName('Site1Thread')
     
     def run(self):
     site1_file = os.path.join(os.path.dirname(os.path.realpath(__file__)),'site.js')
     site2_file = os.path.join(self.outputFilePath,'site.js')
     if not os.path.isfile(site2_file) and os.path.isfile(site1_file):
       shutil.copy(site1_file,site2_file)
     #proc = subprocess.Popen(["bash","-c", "cd %s && ./casperjs site.js --url=http://spys.ru/free-proxy-list/IE/ --outputfile=%s" % (self.outputFilePath,self.fileName) ],stdout=subprocess.PIPE)
     if isWindows():
       proc = subprocess.Popen(["cmd","/c", "%s/casperjs site.js --url=http://spys.ru/free-proxy-list/IE/ --outputfile=%s" % (self.outputFilePath,self.fileName) ],stdout=subprocess.PIPE)
     else:
       proc = subprocess.Popen(["bash","-c", "cd %s && ./casperjs site.js --url=http://spys.ru/free-proxy-list/IE/ --outputfile=%s" % (self.outputFilePath,self.fileName) ],stdout=subprocess.PIPE)
     out=proc.communicate()[0]
     htmlFileName = ''
     #因為輸出路徑在windows不確定,所以這里加了所有可能的路徑判斷
     if os.path.isfile(self.fileName):
       htmlFileName = self.fileName
     elif os.path.isfile(os.path.join(self.outputFilePath,self.fileName)):
       htmlFileName = os.path.join(self.outputFilePath,self.fileName)
     elif os.path.isfile(os.path.join(os.path.dirname(os.path.realpath(__file__)),self.fileName)):
       htmlFileName = os.path.join(os.path.dirname(os.path.realpath(__file__)),self.fileName) 
     if (not os.path.isfile(htmlFileName)):
       print 'Failed to get html content from http://spys.ru/free-proxy-list/IE/'
       print out
       sys.exit(3
     mutex.acquire()
     PROXYList= getTagsByAttrsExt('font',htmlFileName,'class','spy14$')
     for proxy in PROXYList:
       tdContent = proxy.renderContents()
       lineElems = re.split('[<>]',tdContent)
       if re.compile(r'\d+').search(lineElems[-1]) and re.compile('(\d+\.\d+\.\d+)').search(lineElems[0]):
       print lineElems[0],lineElems[-1]
       PROXY_LIST.append("%s:%s" % (lineElems[0],lineElems[-1]))
     mutex.release()
     try:
       if os.path.isfile(htmlFileName):
       os.remove(htmlFileName)
     except:
       pass
    
   if __name__ == '__main__':
     try:
     if(len(sys.argv)) < 2:
       print "Usage:%s [casperjs path]" % (sys.argv[0])
       sys.exit(1
     if not os.path.exists(sys.argv[1]):
       print "casperjs path: %s does not exist!" % (sys.argv[1])
       sys.exit(2
     if os.path.isfile(output_file):
       f = open(output_file)
       lines = f.readlines()
       f.close
       for line in lines:
       PROXY_LIST.append(line.strip())
     thread1 = Site1Thread(sys.argv[1])
     thread1.start()
     thread1.join()
      
     f = open(output_file,'w')
     for proxy in set(PROXY_LIST):
       f.write(proxy+"\n")
     f.close()
     print "Done!"
     except SystemExit:
     pass
     except:
       errMsg = traceback.format_exc()
       print errMsg
       log.error(errMsg)

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品免费在线 | 精一区二区 | 二区影院| 免费观看av | 黄色网在线看 | 日韩精品视频在线观看免费 | chinese国产一区二区 | 国产午夜精品一区二区三区嫩草 | 亚洲精选一区二区 | 国产在线观看一区二区 | 在线观看一区 | 91久久久久久久久 | 欧美精品一二三区 | 超碰97免费在线 | 国产高清精品在线 | 一区二区三区在线播放视频 | 中文字幕在线三区 | 欧美一区二区三 | 欧美一级淫片丝袜脚交 | 欧美亚洲视频 | 欧美一级欧美三级在线观看 | 在线电影亚洲 | 亚洲成人一区 | 欧美综合在线观看 | 久久久精品国产99久久精品芒果 | 91精品久久久久久久久 | 91久久夜色精品国产网站 | 精品一区亚洲 | 韩日中文字幕 | 精品小视频 | 精品国产乱码久久久久久1区2区 | 国产中文字幕一区 | 亚洲一区二区三区视频 | 成人伊人网 | 91香蕉视频在线 | 亚洲精品一级 | 国产精品美女久久久久久久久久久 | 亚洲欧美精品一区二区三区 | 青青草国产 | 免费看黄色小视频 | 久久亚洲欧美日韩精品专区 |