本文整理汇总了Python中urllib2.Request.close方法的典型用法代码示例。如果您正苦于以下问题:Python Request.close方法的具体用法?Python Request.close怎么用?Python Request.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类urllib2.Request
的用法示例。
在下文中一共展示了Request.close方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __open
# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import close [as 别名]
def __open(self, url):
req = Request(url, headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive'})
start = dt.now()
req = None
try:
req = urlopen(url)
self.html_src = req.read()
except Exception as e:
self.req_err = e
else:
if self.req_err is not None:
req.close()
self.req_time = dt.now() - start
return self.html_src
示例2: urlopen
# 需要导入模块: from urllib2 import Request [as 别名]
# 或者: from urllib2.Request import close [as 别名]
print 'Logged in to FitDay. Saved cookie to '+COOKIE_FILE+'.'
# Download weight data
nexturl = 'https://www.fitday.com/fitness/WeightHistory.html'
while nexturl != '':
# Load the cookie and visit the URL of interest.
cookieJar.load(COOKIE_FILE)
while True:
try:
req = urlopen(nexturl)
break
except:
print "Error reading the URL. Trying again..."
htmlSource = req.read()
req.close()
# Filter the result a bit.
htmlSource = htmlSource[11:]
htmlSource = htmlSource.replace('\\"','"')
htmlSource = htmlSource.replace('\\n','\n')
htmlSource = htmlSource.replace('\\t','\t')
soup = BeautifulSoup(''.join(htmlSource))
to_extract = soup.findAll('script') # removing JS
for item in to_extract:
item.extract()
weight_table = soup.find('div', {'class' : 'ListView'}).table