def sendRequest(self): url = '%s://%s/%s?sig=%s' % (self.PROTOCOL, self.HOST, self.ENDPOINT, self.getSignature()) try: #req() is urllib2.Request() request = req(url, self.getRequest()) self.response = urlopen(request) except Exception, e: pass
def sendRequest(self): url = '%s://%s/%s?sig=%s' % (self.PROTOCOL, self.HOST, self.ENDPOINT, self.getSignature()) try: #req() is urllib2.Request() request = req(url, self.getRequest()) self.response = urlopen(request) except: pass #mc.GetApp().Close() try: self.decoded = json.loads(self.response.read()) except: self.error = 9000 self.message = 'malformed response' return if self.decoded.has_key('errors'): self.code = self.decoded['errors'][0]['code'] self.message = self.decoded['errors'][0]['message']
from urllib2 import urlopen as ureq from urllib2 import Request as req from bs4 import BeautifulSoup as soup import lxml agent='Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/1.0.154.53 Safari/525.19' headers={ "User-Agent":agent } url='https://www.google.com/search?q=pangolin' #"User-Agent":agent html=req(url, headers=headers) try: gold=ureq(html); #url.add_header('User-Agent','Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/1.0.154.53 Safari/525.19') #page_soup=open('html') page_soup=soup(gold, "lxml") divs=page_soup.findAll("div", {"class":"rc"}) for div in divs: gold=div.h3.a print gold.text except urllib2HTTPError, e: print "HTTP error:", e.code exit(1)
def create_page_soup(url): client_response = req(url) page_html = client_response.read() client_response.close() return soup(page_html, "html.parser")