1. 程式人生 > >Xpath 和 BeautifulSoup4區別對比

Xpath 和 BeautifulSoup4區別對比

XPath

1. 永遠返回一個列表:有資料的列表 或 空列表

2. XPath匹配時,下標從 1 開始

3. XPath取值的 目標值 兩種:
-1. 指定標籤的文字內容 (如取文字)
-2. 指定標籤的指定屬性值 (如取連結)

XPath取出的字串資料,都是Unicode編碼字串。

4. 如果取值的目標值很多,可以先獲取所有結點列表,再迭代取值:
獲取結點列表

node_list = "//div[@class='f18 mb20']"

for node in node_list:
  item = {}
  item['text'] = " ".join(ode.xpath("./text()"))
  item['a_text'] = node.xpath("./a/text()")[0]
  item['link'] = node.xpath("./a/@href")[0]

html = response.read()
html = response.content

#匯入lxml類庫裡的 etree模組
from lxml import etree
 通過 etree模組的 HTML類 獲取 HTML DOM物件
html_obj = etree.HTML(html)
 html_obj = etree.parse("./baidu.html")
 html = etree.tostring(html_obj)

node_list = html_obj.xpath("//div[@class='f18 mb20']/a/@href")

BeautifulSoup4 的常用匹配方法:

1. find() : 匹配網頁中第一個符合規則的結果,並返回該結果
2. find_all() :匹配網頁中所有符合規則的結果,並返回結果列表
find() 和 find_all() 語法相同
3. select() : 匹配網頁中所有符合規則的結果,並返回結果列表(使用CSS選擇器用法)

url = "https://hr.tencent.com/position.php?&start=0" += 10


item_list = []
node_list = soup.find_all("tr", {"class" : ["even", "odd"]})

for node in node_list:
    item = {}
    item['position_name'] = node.find_all("td")[0].a.text
    item['position_link'] = node.find_all("td")[0].a.get("href")
    item['position_type'] = node.find_all("td")[1].text
    item['people_number'] = node.find_all("td")[2].text
    item['work_location'] = node.find_all("td")[3].text
    item['publish_times'] = node.find_all("td")[4].text
    item_list.append(item)

Xpath 和bs4使用對比:

import requests
from lxml import etree
from bs4 import BeautifulSoup
url = "https://hr.tencent.com/position.php?&start=10"
headers = {"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"}
html = requests.get(url, headers=headers).content

html_obj = etree.HTML(html)
html_obj.xpath("//tr[@class='even']")
html_obj.xpath("//tr[@class='odd']")
html_obj.xpath("//tr[@class='even'] | //tr[@class='odd']")

soup = BeautifulSoup(html, "lxml")
soup.find_all("tr")
# 找出所有的tr
len(soup.find_all("tr"))
# 找出所有指定屬性的 tr
len(soup.find_all("tr", {"class" : "even"}))
len(soup.find_all("tr", {"class" : "odd"}))
len(soup.find_all("tr", {"class" : ["even", "odd"]}))

# 找出所有指定屬性的 tr 和tmm,屬性相同
len(soup.find_all(["tr", "tmm"], {"class" : ["even", "odd"]}))
# 根據屬性查詢所有指定的標籤
len(soup.find_all(attrs={"class" : ["even", "odd"]}))
# 根據class屬性超找所有指定的標籤
len(soup.find_all(class_ = ["even", "odd"]))

# 找出所有class為 even 和 odd 的標籤
len(soup.select(".even"))
len(soup.select(".even, .odd"))
len(soup.select("[class='even'], [class='odd']"))


bs4提取文字和屬性值:

import requests
from bs4 import BeautifulSoup
url = "https://hr.tencent.com/position.php?&start=10"
headers = {"User-Agent" : "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"}
html = requests.get(url, headers=headers).content
soup = BeautifulSoup(html, "lxml")

node_list = soup.find_all("tr", {"class" : ["even", "odd"]})

node_list[0].td
node_list[0].find_all("td")
node_list[0].select("td")

node_list[0].select("td")[0]
node_list[0].select("td")[0].a

node_list[0].select("td")[0].a.string
node_list[0].select("td")[0].a.text
node_list[0].select("td")[0].a.get_text()

node_list[0].select("td")[0].a.get("href")
node_list[0].select("td")[0].a.attrs
node_list[0].select("td")[0].a.attrs["href"]