Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

RegEx in python questions


elmiguel's Avatar
Member
2,795 1

Hello I have been trying to do the timed challenges in JS. I have completed up to 6. I tried doing timed 6 in JS but trying to get the XMLHttpRequest() to get the data off of google is tricky (unless I'm using the wrong function…?) Anyway, I thought it would be practice to try doing it in a different language, so I chose Python.

I can log into the site and get the page data off of timed6/index.php.

word = re.search("[a-z]{1,}(?=<\/strong>)", hbh)```

side note: I also notice that the word var is not correct but I will fix it later (ex: if word = google then it finds google, but if word = good bye then only bye is found.)

Anyway, when it finds the regex patterns I get:
```markup<_sre.SRE_Match object at 0x00E42368>```

How can I print this in the shell so I see the actual word not its location?


elmiguel's Avatar
Member
2,795 1

Never mind, using findall() function.


elmiguel's Avatar
Member
2,795 1

fixed the findall() strings to

word = re.findall("\w+(?=<\/strong> and post)", hbh)```

looks neater but stills doesn't grab the spaces if in ex: "denial of service" only gets service.

elmiguel's Avatar
Member
2,795 1

Ok, if I have a page opened and I request another, how can I store that data?

for example:

reqPage = 'http://www.site.com'
req2 = urllib2.Request(reqPage, None)
pData = #how do I search through this request for my data?

Demons Halo's Avatar
Member
0 0

elmiguel wrote: Ok, if I have a page opened and I request another, how can I store that data?

for example:

reqPage = 'http://www.site.com'
req2 = urllib2.Request(reqPage, None)
pData = #how do I search through this request for my data?

1- To store the data you need to receive it first as you know, and then store it somewhere (a variable maybe (?))

2- to search through the request:

req = Request('www.blabla.com', None, headers)  #headers is where cookies goes  
response = urlopen(req)      #open the site
site2string = str(response.read())  #reading the responce as a single string
find_data = site2string.find('YOUR DATA MOHAHAHAH')    #search for your data

http://www.java2s.com/Code/Python/String/Stringsearchsearchforposition.htm


elmiguel's Avatar
Member
2,795 1

cool thanks reading now,


elmiguel's Avatar
Member
2,795 1

It seems like I am having trouble retrieving data from here:

google = 'http://www.google.co.uk/search?q=' + word[0]

if I just do http://www.google.com and store the data its does it fine, but not when I do it with search?q= and the end of it.

Any suggestions?…


ghost's Avatar
0 0

Wow, you posted over 85% of the posts in this thread.


elmiguel's Avatar
Member
2,795 1

sad isn't……

But I have fixed most of my issues so far I am trying to figure out how to print out an arrays' index though a var

ex:

intLink = 2
print theLinks[intLink]

gives and error in python:

  File "I:/HBH Work Files/timed6_pyA.py", line 32, in <module>
    print theLinks[intLink]
TypeError: list indices must be integers, not str```

elmiguel's Avatar
Member
2,795 1

hmm weird none of them are working. I have tried all the examples, but for some reason it will not take a variable as an integer it reads it as a string. And for some reason my last for loop is not running….


Demons Halo's Avatar
Member
0 0

well it works when I do it :S

post your code and let me have a look =)


SySTeM's Avatar
-=[TheOutlaw]=-
20 0

Demons Halo wrote: well it works when I do it :S

post your code and let me have a look =)

He can't really post his code as it's for beating a mission, if he does then people can just copy it and use it themselves :-/


Demons Halo's Avatar
Member
0 0

system_meltdown wrote: [quote]Demons Halo wrote: well it works when I do it :S

post your code and let me have a look =)

He can't really post his code as it's for beating a mission, if he does then people can just copy it and use it themselves :-/[/quote]

well he can either pm me or just post the loop that is not working so someone could help him fix the syntax =P


elmiguel's Avatar
Member
2,795 1

I can post an example of what I am trying to do:

#Python 2.6.1
intNum = 2 #the random number using 2 for purpose

aArray = ['item1','item2','item3'] # etc, etc..

print aArray[intNum]  #cannot work because its reading intNum as a string

Since I do not know the number right a way nor will it work if I hard code it, I need it to grab the random number that is given. The only way that I know of is to store it in a variable. Which is not hard, but, I need to know how to pass the variable in the index as a integer not a string.


ghost's Avatar
0 0

Isn't it possible to do int(intNum) to get it as an integer?


Demons Halo's Avatar
Member
0 0
>>> wtf = 2
>>> list= ['bla 1', 'bla 2', 'bla 3']
>>> print list[wtf]
bla 3```

and in case the number you're grabbing is a tiny bit of a string, then use:

```markup
int(variable) #This will convert the string into integer, make sure you have only numbers as a value of this variable

str(variable)  #To convert a variable value into a string

elmiguel's Avatar
Member
2,795 1

You Rock!!!!!


elmiguel's Avatar
Member
2,795 1

Ok, I am at the final stage of this challenge, all I need help with now is how to post back the data to the same url…..

do I have to subit another request? (I do not think that would be it right, wouldn't it just grab a new page?)

do I just open the page again and post back? (not sure if python keeps it open or not)


ghost's Avatar
0 0

elmiguel wrote: Ok, I am at the final stage of this challenge, all I need help with now is how to post back the data to the same url…..

do I have to subit another request? (I do not think that would be it right, wouldn't it just grab a new page?)

do I just open the page again and post back? (not sure if python keeps it open or not) Basic http. Learn it.


Demons Halo's Avatar
Member
0 0

COM wrote: Basic http. Learn it.

This has nothing to do with his question =P (as far as I know ^^)…

simply you need to request a new page with your answer added to the url using the given keys in the challenge description.


ghost's Avatar
0 0

Demons Halo wrote: [quote]COM wrote: Basic http. Learn it.

This has nothing to do with his question =P (as far as I know ^^)…

simply you need to request a new page with your answer added to the url using the given keys in the challenge description.[/quote] If you'd have learnt basic http then you would've known that it had a fucking lot to do with his question, goddamn substitute teacher.


Demons Halo's Avatar
Member
0 0

COM wrote: [quote]Demons Halo wrote: [quote]COM wrote: Basic http. Learn it.

This has nothing to do with his question =P (as far as I know ^^)…

simply you need to request a new page with your answer added to the url using the given keys in the challenge description.[/quote] If you'd have learnt basic http then you would've known that it had a fucking lot to do with his question, goddamn substitute teacher.[/quote]

:( rofl… substitute teacher xD stop shoving this basic http stuff up everybody's ass! =P I know it's a must, but it's sure boring as hell …

I love you btw…


ghost's Avatar
0 0

Demons Halo wrote: :( rofl… substitute teacher xD stop shoving this basic http stuff up everybody's ass! =P I know it's a must, but it's sure boring as hell …

I love you btw…

I'll stop shoving it up everybody's ass when they stop bending over for it. That includes you and please, shave that hairy ass of yours. The http will get lost on its way in, which is probably why it still hasn't reached you.


elmiguel's Avatar
Member
2,795 1

Wow thanks COM I don't know if I ever would have figured that out.. Wow you are amazing!!


Demons Halo's Avatar
Member
0 0

COM wrote: I'll stop shoving it up everybody's ass when they stop bending over for it. That includes you and please, shave that hairy ass of yours. The http will get lost on its way in, which is probably why it still hasn't reached you.

hahahahaha fair enough! but how did you know my ass is hairy? :S did chocolate tell you?


ghost's Avatar
0 0

elmiguel wrote: Wow thanks COM I don't know if I ever would have figured that out.. Wow you are amazing!! I know I am, good thing I'm here to save you. Who knows where you'd ever be without my help, support and assistance. Knowing how immensely great, generous and awesome I am, I hereby allow you to build a small shrine in my honor where you can come to worship me.


ghost's Avatar
0 0

Thanks, COM. You made my day a little merrier. :)