Python connects to Redis and inserts URLs

Source: Internet
Author: User
Tags connection pooling

#!/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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.