中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

如何做淘客網(wǎng)站源碼百度云搜索引擎入口官網(wǎng)

如何做淘客網(wǎng)站源碼,百度云搜索引擎入口官網(wǎng),阿拉巴巴開店網(wǎng)站建設(shè),上海羽貝網(wǎng)站建設(shè)在前面的例子用,我用了BeautifulSoup來從58同城抓取了手機(jī)維修的店鋪信息,這個(gè)庫使用起來的確是很方便的。本文是BeautifulSoup 的一個(gè)詳細(xì)的介紹,算是入門把。文檔地址:http://www.crummy.com/software/BeautifulSoup/bs4/doc/ …

在前面的例子用,我用了BeautifulSoup來從58同城抓取了手機(jī)維修的店鋪信息,這個(gè)庫使用起來的確是很方便的。本文是BeautifulSoup 的一個(gè)詳細(xì)的介紹,算是入門把。文檔地址:http://www.crummy.com/software/BeautifulSoup/bs4/doc/

什么是BeautifulSoup?

Beautiful Soup 是用Python寫的一個(gè)HTML/XML的解析器,它可以很好的處理不規(guī)范標(biāo)記并生成剖析樹(parse tree)。 它提供簡單又常用的導(dǎo)航(navigating),搜索以及修改剖析樹的操作。它可以大大節(jié)省你的編程時(shí)間。

直接看例子:

#!/usr/bin/python# -*- coding: utf-8 -*-from bs4 import BeautifulSouphtml_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p><p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p><p class="story">...</p>"""soup = BeautifulSoup(html_doc)print soup.titleprint soup.title.nameprint soup.title.stringprint soup.pprint soup.aprint soup.find_all('a')print soup.find(id='link3')print soup.get_text()結(jié)果為:<title>The Dormouse's story</title>
title
The Dormouse's story
<p class="title"><b>The Dormouse's story</b></p>
<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
<a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>The Dormouse's story
The Dormouse's story
Once upon a time there were three little sisters; and their names were
Elsie,
Lacie and
Tillie;
and they lived at the bottom of a well.

可以看出:soup 就是BeautifulSoup處理格式化后的字符串,soup.title 得到的是title標(biāo)簽,soup.p 得到的是文檔中的第一個(gè)p標(biāo)簽,要想得到所有標(biāo)簽,得用find_all

函數(shù)。find_all 函數(shù)返回的是一個(gè)序列,可以對它進(jìn)行循環(huán),依次得到想到的東西.

get_text() 是返回文本,這個(gè)對每一個(gè)BeautifulSoup處理后的對象得到的標(biāo)簽都是生效的。你可以試試 print soup.p.get_text()

其實(shí)是可以獲得標(biāo)簽的其他屬性的,比如我要獲得a標(biāo)簽的href屬性的值,可以使用 print soup.a[‘href’],類似的其他屬性,比如class也是可以這么得到的(soup.a[‘class’])。

特別的,一些特殊的標(biāo)簽,比如head標(biāo)簽,是可以通過soup.head 得到,其實(shí)前面也已經(jīng)說了。

如何獲得標(biāo)簽的內(nèi)容數(shù)組?使用contents 屬性就可以 比如使用 print soup.head.contents,就獲得了head下的所有子孩子,以列表的形式返回結(jié)果,

可以使用 [num] 的形式獲得 ,獲得標(biāo)簽,使用.name 就可以。

獲取標(biāo)簽的孩子,也可以使用children,但是不能print soup.head.children 沒有返回列表,返回的是 <listiterator object at 0x108e6d150>,

不過使用list可以將其轉(zhuǎn)化為列表。當(dāng)然可以使用for 語句遍歷里面的孩子。

關(guān)于string屬性,如果超過一個(gè)標(biāo)簽的話,那么就會(huì)返回None,否則就返回具體的字符串print soup.title.string 就返回了 The Dormouse’s story

超過一個(gè)標(biāo)簽的話,可以試用strings

向上查找可以用parent函數(shù),如果查找所有的,那么可以使用parents函數(shù)

查找下一個(gè)兄弟使用next_sibling,查找上一個(gè)兄弟節(jié)點(diǎn)使用previous_sibling,如果是查找所有的,那么在對應(yīng)的函數(shù)后面加s就可以

如何遍歷樹?

使用find_all 函數(shù)

find_all(name, attrs, recursive, text, limit, **kwargs)

舉例說明:

print soup.find_all('title')
print soup.find_all('p','title')
print soup.find_all('a')
print soup.find_all(id="link2")
print soup.find_all(id=True)

返回值為:

[<title>The Dormouse's story</title>]
[<p class="title"><b>The Dormouse's story</b></p>]
[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
[<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]
[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]

通過css查找,直接上例子把:

print soup.find_all(“a”, class_=“sister”)
print soup.select(“p.title”)

通過屬性進(jìn)行查找
print soup.find_all(“a”, attrs={“class”: “sister”})

通過文本進(jìn)行查找
print soup.find_all(text=“Elsie”)
print soup.find_all(text=[“Tillie”, “Elsie”, “Lacie”])

限制結(jié)果個(gè)數(shù)
print soup.find_all(“a”, limit=2)

結(jié)果為:

[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
[<p class="title"><b>The Dormouse's story</b></p>]
[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>]
[u'Elsie']
[u'Elsie', u'Lacie', u'Tillie']
[<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>]

總之,通過這些函數(shù)可以查找到想要的東西。

http://www.risenshineclean.com/news/22211.html

相關(guān)文章:

  • 2022年免費(fèi)網(wǎng)站軟件下載seo是什么崗位的縮寫
  • 代碼網(wǎng)站模板怎么做今日頭條荊州新聞
  • 網(wǎng)頁設(shè)計(jì)與網(wǎng)站建設(shè)教材微信營銷的模式有哪些
  • 企業(yè)自建站案例搜索關(guān)鍵詞推薦
  • 營業(yè)執(zhí)照辦好了就可以做網(wǎng)站了嗎廣東云浮疫情最新情況
  • 花生殼域名可以做網(wǎng)站域名嗎3322免費(fèi)域名注冊
  • 平面設(shè)計(jì)素材網(wǎng)站排名武漢搜索排名提升
  • 購物網(wǎng)站開發(fā)實(shí)例網(wǎng)絡(luò)服務(wù)提供商
  • wordpress多國語言設(shè)置淘寶seo是什么意思
  • 網(wǎng)站制作 臺(tái)州淘寶代運(yùn)營1個(gè)月多少錢
  • 一個(gè)做搞笑類視頻的網(wǎng)站取名seo學(xué)習(xí)網(wǎng)站
  • 北京it行業(yè)公司排名關(guān)于華大18年專注seo服務(wù)網(wǎng)站制作應(yīng)用開發(fā)
  • 怎么用一個(gè)主機(jī)做多個(gè)網(wǎng)站軟文之家
  • 網(wǎng)站建設(shè)合同英文版seo 優(yōu)化技術(shù)難度大嗎
  • 眉山網(wǎng)站制作最吸引人的引流話術(shù)
  • 推廣公司如何賺錢網(wǎng)站優(yōu)化策劃書
  • 做界面的網(wǎng)站12月30日疫情最新消息
  • 南平公司做網(wǎng)站pc網(wǎng)站建設(shè)和推廣
  • 文案交流網(wǎng)站免費(fèi)網(wǎng)站大全下載
  • 2015做哪些網(wǎng)站致富什么是互聯(lián)網(wǎng)營銷
  • 有阿里空間怎么做網(wǎng)站百度推廣后臺(tái)登錄頁面
  • 網(wǎng)站廣告位價(jià)格一般多少定制化網(wǎng)站建設(shè)
  • 自己做的網(wǎng)站可以掛在哪里自己怎么做網(wǎng)頁推廣
  • 網(wǎng)站關(guān)鍵詞詞庫app拉新推廣
  • 怎么做網(wǎng)站首頁psd整站優(yōu)化服務(wù)
  • 全球最受歡迎的網(wǎng)站排名今日熱點(diǎn)新聞
  • 哪個(gè)網(wǎng)站可以做思維導(dǎo)圖鳳山網(wǎng)站seo
  • 做美圖 網(wǎng)站有哪些東西沈陽優(yōu)化網(wǎng)站公司
  • 蒙城做網(wǎng)站的公司百度手機(jī)助手下載正版
  • 響應(yīng)式網(wǎng)站建設(shè)資訊培訓(xùn)行業(yè)seo整站優(yōu)化