python 爬取標簽內href的方法及遇到的問題


原博客地址:

https://www.cnblogs.com/dengyg200891/p/6060010.html

 1 # -*- coding:utf-8 -*-
 2 #python 2.7
 3 #XiaoDeng
 4 #http://tieba.baidu.com/p/2460150866
 5 #標簽操作
 6 
 7 
 8 from bs4 import BeautifulSoup
 9 import urllib.request
10 import re
11 
12 
13 #如果是網址,可以用這個辦法來讀取網頁
14 #html_doc = "http://tieba.baidu.com/p/2460150866"
15 #req = urllib.request.Request(html_doc)  
16 #webpage = urllib.request.urlopen(req)  
17 #html = webpage.read()
18 
19 
20 
21 html="""
22 <html><head><title>The Dormouse's story</title></head>
23 <body>
24 <p class="title" name="dromouse"><b>The Dormouse's story</b></p>
25 <p class="story">Once upon a time there were three little sisters; and their names were
26 <a href="http://example.com/elsie" class="sister" id="xiaodeng"><!-- Elsie --></a>,
27 <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
28 <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
29 <a href="http://example.com/lacie" class="sister" id="xiaodeng">Lacie</a>
30 and they lived at the bottom of a well.</p>
31 <p class="story">...</p>
32 """
33 soup = BeautifulSoup(html, 'html.parser')   #文檔對象
34 
35 
36 #查找a標簽,只會查找出一個a標簽
37 #print(soup.a)#<a class="sister" href="http://example.com/elsie" id="xiaodeng"><!-- Elsie --></a>
38 
39 for k in soup.find_all('a'):
40     print(k)
41     print(k['class'])#查a標簽的class屬性
42     print(k['id'])#查a標簽的id值
43     print(k['href'])#查a標簽的href值
44     print(k.string)#查a標簽的string
45     #tag.get('calss'),也可以達到這個效果

 

在使用該方法的k['href']讀取網頁鏈接時,編譯器報錯:

 KeyError: 'href'

修改為:

 k.get('href')

成功運行,取出href中的鏈接。



 



 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM