Read oreilly.web.scraping.with.python.2015.6 Notes---Crawl

Source: Internet
Author: User

Read oreilly.web.scraping.with.python.2015.6 Notes---Crawl

1. The function calls itself, so that a loop is formed and a loop is set:

From urllib.request import urlopenfrom bs4 import Beautifulsoupimport repages = set () def getlinks (pageurl): Global page s HTML = urlopen ("http://en.wikipedia.org" +pageurl) bsobj = BeautifulSoup (HTML, "lxml") Try:print (bsobj.h1 . Get_text ()) print (Bsobj.find (id = "Mw-content-text"). FindAll ("P") [0])//Find page in Id=mw-content-tex T, and then find "P" on this basis the contents of this label [0] represent the selection of the No. 0 print (Bsobj.find (id= "Ca-edit"). Find ("span"). FIND ("a"). attrs[' href '])//Find To id=ca-edit inside the span tag inside the A tag inside the value of the href except Attributeerror:print ("This page is missing something!            No worries though! ") For link in Bsobj.findall ("A", Href=re.compile ("^ (/wiki/)")): if ' href ' in link.attrs:if link.attrs[' hr                EF '] Not in pages: #We has encountered a new page newPage = link.attrs[' href '] Print (NewPage) pages.add (newPage) getlinks (newPage) getlinks ("")

2. Processing the URL, using "/" to split the characters in the URL

def splitaddress (address):    addressparts = Address.replace ("/", "" "). Split ("/")    return addresspartsaddr = Splitaddress ("https://hao.360.cn/?a1004") print (addr)

The result of the operation is:

Runfile (' c:/users/user/desktop/chensimin.py ', wdir= ' c:/users/user/desktop ') [' https: ', ' ', ' hao.360.cn ', '? a1004 ']                   //two///no content, used ' means '

  

def splitaddress (address):    addressparts = Address.replace ("/", "" "). Split ("/")    return addresspartsaddr = Splitaddress ("http://www.autohome.com.cn/wuhan/#pvareaid =100519") print (addr)

The result of the operation is:

Runfile (' c:/users/user/desktop/chensimin.py ', wdir= ' c:/users/user/desktop ') [' www.autohome.com.cn ', ' Wuhan ', ' # pvareaid=100519 ']

Read oreilly.web.scraping.with.python.2015.6 Notes---Crawl

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.