Python網路資料擷取二

來源:互聯網
上載者:User

標籤:pen   lin   .com   urlopen   attr   def   ret   int   dal   

一、Regex

 *     表匹配0次或者多次  a*b*

+    表至少一次

[ ]   匹配任意一個

( )  辨識一個編組

{m,n} m或者n 次

[^]  匹配任意不在中括弧裡的字元

|    表示或者

.    表示匹配任一字元

^    表字元的開始 ^a  表示以a開始

\    表示逸出字元

$  和^ 相反  從字串的末尾開始匹配

?! 不包含

二、獲得屬性

獲得一個標籤的全部屬性

myTag.attrs

擷取圖片的資源位置src

myImgTag.attrs["src"]

 

 

擷取網頁的函數:

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網路資料擷取二

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.