Python. beatifulsoup, pythonsoup

Source: Internet
Author: User

Python. beatifulsoup, pythonsoup

First, I want to try the python crawler function. So I installed beautifulsoup with easy_install. below is the demo code I wrote. Let's take a look.

# Coding = utf-8import urllib2from BeautifulSoup import BeautifulSoup as bsurl_addr = 'HTTP: // your save_to_file (content, fileName): with open (fileName, 'W + ') as f: if not f: return Falsef. write (content) f. close () def parse_content (content): soup = bs (content) tds = soup. findAll ('td ') for I in tds: print I. textdef is_unicode (c): if isinstance (c, unicode): return Truereturn Falsedef is_asii (c): if isinstance (c, str ): return Truereturn Falseif _ name _ = '_ main _': content = urllib2.urlopen (url_addr ). read () # str in any format is converted to the intermediate encoding unicode # Call decode to convert it to the unicode encoding [according to its own encoding] # Then call encode to convert unicode to the specified encoding # convert content [encoding for gb2312] to unicode encoding c = content. decode ('gb2312') print ('C is unicode: ', is_unicode (c) # print (c) save_to_file (c. encode ('gb2312'), 'd:/content.html ') parse_content (content)


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.