Python for Android:beautifulsoup has a bug

Source: Internet
Author: User

BeautifulSoup is good at web data analysis, but Python for Android:beautifulsoup has bugs,

Text = H4.a.text can only get None, so I wrote the Function:gettext () to fix this bug.

Example: Grabbing csdn geek headlines soup.py

Import Urllib2, refrom beautifulsoup import beautifulsoupimport sysreload (SYS) sys.setdefaultencoding (' Utf-8 ') def GetText (text):    begin = Text.find (' > ', 0)    if begin >-1:        begin + = 1        end = Text.find (' </a> ', Begin        If begin < end:            return Text[begin:end].strip ()        else:            return None    else:        return Nonepage = Urllib2.urlopen ("http://geek.csdn.net/new") soup = beautifulsoup (page) for H4 in Soup.findall (' H4 '):    if H4.A is not None:        href = h4.a.get (' href ')        text = GetText (str (h4.a))        print text        print Hrefpage.close ()

please refer to: Http://www.crummy.com/software/BeautifulSoup/bs3/documentation.zh.html

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.