最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實現(xiàn)獲取網(wǎng)頁信息并解析

 更新時間:2025年05月22日 10:50:58   作者:KevinQ  
這篇文章主要為大家詳細介紹了如何使用Python實現(xiàn)獲取網(wǎng)頁信息并解析功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習一下

Python爬蟲用到的兩個主要的庫是:bs4和request,request用于發(fā)起請求,而bs4用于網(wǎng)頁元素解析。

以阮一峰老師的博客為例,每周最喜歡的是科學(xué)愛好者周刊中的“言論”不分,以 科技愛好者周刊(第 253 期)為例,讓我們來看看能不能將言論部分提取出來。

import requests  
from bs4 import BeautifulSoup  
  
url = "http://www.ruanyifeng.com/blog/2023/05/weekly-issue-253.html"  
response = requests.get(url)  
soup = BeautifulSoup(response.content, "html.parser")  
first_tag = soup.find("h2", string="言論")  
next_sibling = first_tag.find_next_sibling()  
content1 = ""  
while next_sibling.name != "h2":  
    content1 += str(next_sibling.get_text())  
    # content1 += str(next_sibling)  
    content1 += "\n\n"  
    next_sibling = next_sibling.find_next_sibling()  
print(content1)

執(zhí)行結(jié)果:

用到的重要函數(shù)是查找某個tag,獲取某個tag的下一個tag函數(shù):

find與find_all

函數(shù)定義如下:

def find(self, name=None, attrs={}, recursive=True, text=None,  
         **kwargs):  
    """Look in the children of this PageElement and find the first  
    PageElement that matches the given criteria.  
    All find_* methods take a common set of arguments. See the online    documentation for detailed explanations.  
    :param name: A filter on tag name.    :param attrs: A dictionary of filters on attribute values.    :param recursive: If this is True, find() will perform a        recursive search of this PageElement's children. Otherwise,        only the direct children will be considered.    :param limit: Stop looking after finding this many results.    :kwargs: A dictionary of filters on attribute values.    :return: A PageElement.  
    :rtype: bs4.element.PageElement  
    """    
    r = None  
    l = self.find_all(name, attrs, recursive, text, 1, **kwargs)  
    if l:  
        r = l[0]  
    return r
def find_all(self, name=None, attrs={}, recursive=True, text=None,  
             limit=None, **kwargs):  
    """Look in the children of this PageElement and find all  
    PageElements that match the given criteria.  
    All find_* methods take a common set of arguments. See the online    documentation for detailed explanations.  
    :param name: A filter on tag name.    :param attrs: A dictionary of filters on attribute values.    :param recursive: If this is True, find_all() will perform a        recursive search of this PageElement's children. Otherwise,        only the direct children will be considered.    :param limit: Stop looking after finding this many results.    :kwargs: A dictionary of filters on attribute values.    :return: A ResultSet of PageElements.  
    :rtype: bs4.element.ResultSet  
    """    
    generator = self.descendants  
    if not recursive:  
        generator = self.children  
    return self._find_all(name, attrs, text, limit, generator, **kwargs)

find 返回的是一個元素,find_all返回的是一個列表,舉例說明比較清晰。

允許傳入的參數(shù)包括:

1.字符串:tag的名稱,如h2, p, b, a等等分別表示查找<h2>, <p>, <b>, <a>等標簽。 如:

soup.find_all('b')
# [<b>這里加粗</b>]

2.正則表達式

# 導(dǎo)入包
import re
for tag in soup.find_all(re.compile("^b")):
    print(tag.name)
# 結(jié)果會找出 body, b等b開頭的標簽

.3列表:與列表中任一元素匹配的內(nèi)容返回

soup.find_all(["a", "b"])
# 輸出: [<b>加粗</b>,
#  <a class="ddd" href="http://xxx" rel="external nofollow" >xxx</a> ]

4.True: 返回所有非字符串節(jié)點。

5.方法:傳入的方法接受唯一參數(shù):元素,并返回True或者False,若元素計算的值為True,則返回。

# 判斷一個tag有class屬性,但是沒有id屬性
def has_class_but_no_id(tag):
    return tag.has_attr('class') and not tag.has_attr('id')
# 使用方式
soup.find_all(has_class_but_no_id)

6.對元素指定判斷函數(shù):

# 查找所有href標簽不是https的a標簽
def not_https(href):
        return href and not re.compile("https").search(href)
soup.find_all(href=not_https)

通過上述第5種和第6種方法,可以構(gòu)造很復(fù)雜的tag過濾函數(shù),從而實現(xiàn)過濾目的。

其他相關(guān)搜索函數(shù)如下:

find_next_sibling 返回后面的第一個同級tag節(jié)點 find_previous_sibling 返回前面的第一個同級tag節(jié)點 find_next 后面第一個tag節(jié)點 find_previous 前面第一個tag節(jié)點

更多內(nèi)容可以在bs4官方文檔中查看。

到此這篇關(guān)于Python實現(xiàn)獲取網(wǎng)頁信息并解析的文章就介紹到這了,更多相關(guān)Python獲取網(wǎng)頁信息內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

潼关县| 麻江县| 东乌珠穆沁旗| 固阳县| 铜川市| 长宁县| 丰顺县| 江永县| 蓝田县| 汤阴县| 珠海市| 仙居县| 柏乡县| 河东区| 色达县| 固镇县| 玛多县| 华宁县| 沾益县| 永新县| 阿合奇县| 江都市| 三明市| 岳西县| 江都市| 尖扎县| 泰兴市| 电白县| 靖江市| 茶陵县| 溧水县| 天峨县| 苏尼特右旗| 霍邱县| 手机| 乌兰察布市| 黄平县| 盘山县| 伊宁县| 沧州市| 开鲁县|