简体   繁体   English

如何将javascript代码转换为python代码?

[英]how can I transfer the javascript code to python code?

the javascript code is here: javascript代码在这里:

var goat=6111+7380;
var hen=5548+7476^goat;
var seal=2094+4451^hen;
var rat=1687+7000^seal;
var pig=3997+8240^rat;

And I want get the goat , hen seal variable and so on in the python. 我想在python中获取goathen seal变量等。 My python code is here: 我的python代码在这里:

animals = 'var goat=6111+7380;var hen=5548+7476^goat;var seal=2094+4451^hen;var rat=1687+7000^seal;var pig=3997+8240^rat;'
[eval(item.replace('var','').strip()) for item in animals.split(';')]####here is wrong

because eval('goat=6111+7380') is wrong, so how can I make the goal is equal the 6111+7380 ? 因为eval('goat = 6111 + 7380')是错误的,所以如何使目标等于6111+7380

ps: thanks everyone. ps:谢谢大家。 Actually I craw a website: http://pachong.org/ to get the proxy address and the port.But the port is generated by <script>document.write((4513^pig)+15);</script> .And the pig variable is generated by <script type="text/javascript">var goat=6111+7380;var hen=5548+7476^goat;var seal=2094+4451^hen;var rat=1687+7000^seal;var pig=3997+8240^rat;</script> ,but this javascript code change every time when I craw the index website.So I do not know how to get the port value. 实际上我搜寻了一个网站: http://pachong.org/ ://pachong.org/以获取代理地址和端口,但是端口是由<script>document.write((4513^pig)+15);</script>pig变量由<script type="text/javascript">var goat=6111+7380;var hen=5548+7476^goat;var seal=2094+4451^hen;var rat=1687+7000^seal;var pig=3997+8240^rat;</script> ,但是每次我检索索引网站时,此javascript代码都会更改。因此,我不知道如何获取端口值。

###resultstring is something like this '(1646^hen)+19'
def getport(resultstring):
    port = eval(resultstring)
    return port

proxyurl= 'http://www.pachong.org/'
try:
    r = requests.get(proxyurl,timeout=60*4)
except:
    print 'I can not get the date of pachong.org'
if r.status_code != 200:
    print 'the status is not good. status_code is %s' % r.status_code
    return
ht = BeautifulSoup(r.content)
animals = str(ht.head.find_all('script')[-1].text)
[eval(item.replace('var','').strip()) for item in animals.split(';')]###it is wrong here

table = ht.find_all('table', attrs={'class':'tb'})
if not table:
    return
table = table[0]
trs = table.find_all('tr',attrs={'data-type':'high'})
tr = trs[0]
idlestring = tr.find_all('td')[5].text
idlestring = idlestring.replace('\n','').replace(' ','')
if idlestring == u'空闲':
    # proxy_id += 1
    ip = tr.find_all('td')[1].text
    portstring = tr.find_all('td')[2].text
    patt = re.compile(u'document.write\((.*?)\);')
    if re.findall(patt,portstring):
        resultstring = re.findall(patt,portstring)[0]
    else:
        continue
    port = getport(resultstring)
    ip_port = '%s:%s' % (ip, port)
    print 'ip_port is %s' % ip_port

I don't know why you'd want to do this, but this works: 我不知道您为什么要这样做,但这可行:

for item in animals.split(';'):
    exec(item.replace('var','').strip())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM