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
-
Why are you bitwise xor-ing 7476 and seal?NoBugs– NoBugs2014年10月08日 01:37:28 +00:00Commented Oct 8, 2014 at 1:37
-
This is a mess. This is not the way you write in pytho. Lists are created using [] and there is no need of var while defining variable. Indentation is the key in python. Please refer somethings from here effbot.org/zone/librarybook-index.htmAbhi– Abhi2014年10月08日 01:39:55 +00:00Commented Oct 8, 2014 at 1:39
-
@Abhi, I believe the goal is to translate from javascript notation to python notation by string manipulation (unless I misunderstand)JB333– JB3332014年10月08日 01:45:16 +00:00Commented Oct 8, 2014 at 1:45
-
@JB333: AAH! The question is little ambiguous!Abhi– Abhi2014年10月08日 01:47:18 +00:00Commented Oct 8, 2014 at 1:47
-
If the JavaScript follows a pattern, you are far better off parsing it somehow (regular expressions or a JavaScript parser) than trying to convert it to Python.Ry-– Ry- ♦2014年10月08日 01:47:37 +00:00Commented Oct 8, 2014 at 1:47
1 Answer 1
I don't know why you'd want to do this, but this works:
for item in animals.split(';'):
exec(item.replace('var','').strip())
3 Comments
^ in both languages.eval, I should user the exec.Then I get several variables in python by javascript code.