03、爬虫数据解析-bs4解析/xpath解析
创始人
2024-11-14 21:06:45

一、bs4解析

使用bs4解析,需要了解一些html的知识,了解其中一些标签。

安装:pip install bs4

导入:from bs4 import BeautifulSoup

1、使用方式

1、把页面源代码交给BeautifulSoup进行处理,生成bs对象

2、从bs对象中查找数据

(1)find(标签,属性=值):找第一个

(2)findall(标签,属性=值):找全部的

2、实战:拿到上海菜价网蔬菜价格

1、思路

(1)拿到源代码

(2)使用bs4进行解析,拿到数据

2、演示

from bs4 import BeautifulSoup import requests import csv  #拿到数据 url = "http://www.shveg.com/cn/info/list.asp?ID=959"  reps = requests.post(url) reps.encoding="gb2312" f = open("菜价.csv",mode="w",encoding="utf-8") csvwriter = csv.writer(f)  #解析数据 #1、把页面源代码交给BeautifulSoup进行处理,生成bs对象。 #2、从bs对象中查找数据 page = BeautifulSoup(reps.text,"html.parser")#html.parser指定html解析 table = page.find("td", attrs={"class":"intro_font"}) trs = table.find_all("tr")[1:] for tr in trs:     tds = tr.find_all("td")     name = tds[0].text     csvwriter.writerow(name) print("over") reps.close()

3、实战:抓取优美图库图片

(1)需求:拿到优美图库图片的下载地址

(2)思路

a.拿到主页面的源代码,然后提取到子页面的链接地址,href

b.通过href拿到子页面的内容,从子页面找到图片的下载地址,src属性

c.下载图片

import requests from bs4 import BeautifulSoup   url = "https://www.umei.cc/bizhitupian/weimeibizhi/" resp = requests.get(url) resp.encoding="utf-8"   main_page = BeautifulSoup(resp.text,"html.parser") alist = main_page.find("div",attrs={"class":"item_list infinite_scroll"}).find_all("a") for a in alist:     href = "https://www.umei.cc/"+a.get('href')     child_page_resp = requests.get(href)     child_page_resp.encoding="utf-8"     child_main_page = BeautifulSoup(child_page_resp.text,"html.parser")     img = child_main_page.find("div",attrs={"class":"big-pic"}).find("img")     src = img.get("src")      #下载图片     img_resp = requests.get(src)     # img_resp.content   #这里拿到的是字节     img_name = src.split("/")[-1] #拿到url中的最后一个/以后的内容     with open(img_name,mode="wb") as f:         f.write(img_resp.content) #图片的内容写入文件     print("over") resp.close() child_page_resp.close()

二、xpath解析

安装:pip install lxml

导入:from lxml import etree

1、使用方式

tree = etree.parse(html文件)

result = tree.xpath("xpath语法")

2、实战:拿到中国食品网的新闻信息

from lxml import etree import requests  url = "http://food.china.com.cn/node_8003189.htm" resp = requests.get(url)  #解析 tree = etree.HTML(resp.text) divs = tree.xpath('/html/body/div[2]/div[3]/div[1]/div[2]/div[@class="d3_back_list"]')  for div in divs:     title = div.xpath("./p/a/text()")     summary = div.xpath("./span/text()")     time = div.xpath("./b/text()")     print(title)     print(summary)     print(time) resp.close()

相关内容

热门资讯

裸辞做“一人公司”,我后悔了 去年这个时候,一位以色列程序员正在东南亚旅行。他顺手把一个在脑子里转了很久的想法做成了产品,一个让任...
南京建成国内首个Pre-6G试... 4月21日,2026全球6G技术与产业生态大会在南京开幕。全息互动技术展台前,一名远在北京的工作人员...
超梵求职受邀参加“2025抖音... 超梵求职受邀参加“2025抖音巨量引擎成人教育行业生态大会”,探讨分享优质内容传播,服务万千学员。 ...
摩托罗拉Razr 2026(R... IT之家 4 月 22 日消息,摩托罗拉宣布新一代 Razr 折叠手机将于 4 月 29 日在美国发...
库克卸任,特纳斯领航:苹果新纪... 苹果首席执行官蒂姆·库克将卸任,硬件工程主管约翰·特纳斯将接任,苹果公司今天宣布此事。 库克将在夏季...