Python implements web crawler crawl static Web page "code"

Source: Internet
Author: User

#---------------------------------Import---------------------------------------#coding: Utf-8import urllib2;from BeautifulSoup Import beautifulsoup;#---------------------------------------------------------------------------- --def Main (): #抓 usermainurl = "Http://tieba.baidu.com/home/main?id=38b94c4ed8add8bcccabd7d31b22&fr=userbar"; #修改抓取的链接地址 req = urllib2.    Request (Usermainurl);    RESP = Urllib2.urlopen (req);    resphtml = Resp.read (); Print "resphtml=", resphtml; #此处输出所有抓取到的HTML源码 #取 songtastehtmlencoding = "GBK"; #修改编码 <span><span class= "Attribute-name" >chars    ET's format </span></span> soup = BeautifulSoup (resphtml, fromencoding=songtastehtmlencoding);    Foundclassh1user = Soup.find (attrs={"target": "_blank"}); #修改抓取内容 print "foundclassh1user=%s", Foundclassh1user;        if (foundclassh1user): H1userstr = foundclassh1user.string; Print "h1userstr=", h1userstr;############################################################################## #if __name__== "__main__": Main (); 

Grab Category 1 Tags

#eg: Siteurls=soup.findall (' a ')

Grab Category 2 Tags

#eg: Foundclassh1user = Soup.find (attrs={"target": "_blank"});

Grab Category 2 Tags

#foundClassH1user = Soup.find (attrs={"class": "H1user"});


Python implements web crawler crawl static Web page "code"

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.