#!/usr/bin/env python#-*-Coding:utf8-*-ImportRedis" "This connection is disconnected once, the cost source. Port default 6379, do not write R = Redis. Redis (host= ' 127.0.0.1 ', port=6379,password= ' Tianxuroot ') r.set (' name ', ' Root ') print (R.get (' name '). Decode (' UTF8 ')) " "" "connection pooling: When a program creates a data source instance, multiple database connections are created at one time, and the database connections are saved in the connection pool, and when the program requires database access, you do not have to re-create the database connection, but instead remove a free database connection from the connection pool" "Pool= Redis. ConnectionPool (host='127.0.0.1', password='HelloWorld')#Implementing a connection poolR= Redis. Redis (connection_pool=pool) R.set ('Foo','Bar')Print(R.get ('Foo'). Decode ('UTF8'))
fromBs4ImportBeautifulSoupImportRequests fromlxmlImportetreeImportRedispool= Redis. ConnectionPool (host='127.0.0.1', port=6379) R= Redis. Redis (connection_pool=pool)#r = Redis.from_url ("redis://127.0.0.1:6379", Decode_responses=true)defget_urls (URL): Result=requests.get (URL) selector=etree. HTML (result.text) links= Selector.xpath (r'//*[@id = "Archive"]/div/div[2]/p[1]/a[1]/@href') forLinkinchLinks:r.sadd ("first_urlsss", link) next_url=Extract_next_url (Result.text)ifnext_url:get_urls (Next_url)defExtract_next_url (HTML): Soup= BeautifulSoup (HTML,"lxml") Next_url= Soup.select ('a[class= "Next Page-numbers"]') forUrlinchNext_url:url=str (URL) soup= BeautifulSoup (URL,"lxml") Next_url= soup.a["href"] returnNext_urlif __name__=='__main__': URL="http://python.jobbole.com/all-posts/"get_urls (URL)
Python connects to Redis and inserts URLs