no13bus
no13bus

Reputation: 401

how can I transfer the javascript code to python code?

the javascript code is here:

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. My python code is here:

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?

ps: thanks everyone. 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.

###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

Upvotes: 0

Views: 232

Answers (1)

Luke Yeager
Luke Yeager

Reputation: 1430

I don't know why you'd want to do this, but this works:

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

Upvotes: 2

Related Questions