Python Network data Acquisition II

Source: Internet
Author: User

First, regular expression

* Table matches 0 or more times a*b*

+ Table at least once

[] Match any one

() identify a group

{m,n} m or n times

[^] matches any characters that are not in brackets

| Indicate or

. Indicates a match for any character

^ Start of the table character ^a to start with a

\ means escape character

$ and ^ opposite starts at the end of the string

?! does not contain

Second, get the attribute

Get all the properties of a label

Mytag.attrs

Get the resource location of the picture SRC

myimgtag.attrs["SRC"]

To get a Web page function:

Random.seed (Datetime.datetime.now ())
def getlinks (Articleurl):
html = Urlopen ("http://en.wikipedia.org" +articleurl)
BS0BJ = BeautifulSoup (HTML)
return Bs0bj.find ("div", {"id": "bodycontent"}). FindAll ("A", Herf=re.compile ("^ (/wiki/)" (?!:).) *$"))
Links = getlinks ("/wiki/kevin_bacon")
While Len (links) > 0:
Newarticle = Links[random.randint (0,len (links)-1)].attrs["href"]
Print (newarticle)
link = getlinks (newarticle)

Python Network data Acquisition II

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.