How to send a request in a keep-alive "urlopen" session using python?

61 views Asked by At

I'm trying to write a HTML based grabber that can grab a twitter user's all pictures.

I realized that only when we scroll down to the bottom that a GET request would be sent to load more tweet/pictures.

But I have no idea about how to simulate that in a python code. This is my code tho it can only grab the "first page" pictures.

import urllib2
import urllib
import re
import sys
import os
import urllib3

generalurl='https://twitter.com/'
INPUT_id = raw_input('Please input the target userid:')
targetpage = generalurl + INPUT_id + '/media'


page = urllib2.urlopen(targetpage)
fo = open('test0.html','w')
fo.write(page.read())
fo.close()
fo = open('test0.html','r')
pics = re.findall('(https://pbs.twimg.com/media/\S+.jpg)',fo.read())
fo.close()



for everyid in pics:
    open_ = urllib.urlopen(everyid)
    filename = re.findall('https://pbs.twimg.com/media/(\S+.jpg)',everyid)
    f=open(filename[0],'wb')
    f.write(open_.read())
    f.close()
0

There are 0 answers