Directed web crawler

Source: Internet
Author: User

1 ImportRequests2  fromBs4ImportBeautifulSoup3 ImportBS44 5 #Crawl the contents of a directed Web page6 defgethtmltext (URL):7     Try:8r = requests.get (URL, timeout = 30)9 r.raise_for_status ()TenR.encoding =r.apparent_encoding One         returnR.text A     except: -         Print('Error') -  the  - deffillunivlist (ulist, HTML): -Soup = BeautifulSoup (HTML,"Html.parser") -      forTrinchSoup.find ('tbody'). Children:#traversing sub-tags under tbody +         ifIsinstance (TR, bs4.element.Tag):#is a label type, not a string -TDS = TR ('TD')#shorthand, output list +Ulist.append ([Tds[0].string, Tds[1].string, tds[2].string]) A  at  - defprintunivlist (ulist, num): -TPLT ="{0:^10}\t{1:{3}^10}\t{2:^10}" #Use the width is not enough, Chinese space padding, ensure the Chinese alignment -     Print(Tplt.format ("ranking","School Name","Provinces", Chr (12288))) -      forIinchrange (num): -u=Ulist[i] in         Print(Tplt.format (U[0],U[1],U[2],CHR (12288))) -  to defMain (): +Uinfo = [] -URL ='http://www.zuihaodaxue.com/zuihaodaxuepaiming2016.html' theHTML =gethtmltext (URL) * fillunivlist (uinfo, HTML) $Printunivlist (Uinfo, 20)Panax Notoginseng  -Main ()

Directed web crawler

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.