日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python安全攻防---爬虫基础---BeautifulSoup解析

發(fā)布時(shí)間:2023/12/1 python 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python安全攻防---爬虫基础---BeautifulSoup解析 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

0x01 基礎(chǔ)

使用bs4首先要安裝,安裝后導(dǎo)入

import bs4

bs對(duì)象有兩個(gè)方法,一個(gè)是find,另一個(gè)是find_all

  • find(標(biāo)簽名,屬性值):只返回一個(gè),返回也是bs對(duì)象,可以繼續(xù)用find和find_all方法
find(name='table',attrs={'class':'hq_table'})
  • find_all(標(biāo)簽名,屬性值):返回所有符合條件,返回也是bs對(duì)象,可以繼續(xù)用find和find_all方法
find_all(name='tr')

0x02 案例

爬取http://www.xinfadi.com.cn/marketanalysis/0/list/1.shtml這個(gè)頁(yè)面菜價(jià)相關(guān)信息
程序:

import requests import bs4url = 'http://www.xinfadi.com.cn/marketanalysis/0/list/1.shtml'response = requests.get(url) page_content = response.text #print(page_content) bs_page = bs4.BeautifulSoup(page_content,'html.parser') table = bs_page.find(name='table',attrs={'class':'hq_table'}) trs = table.find_all(name='tr') for tr in trs:tds = tr.find_all(name='td')for td in tds:print(td.text,end=' ')print()

page_content是我們獲取網(wǎng)頁(yè)的源碼,bs4.BeautifulSoup(page_content,'html.parser'),html.parser是告訴BeautifulSoup解析什么文件

運(yùn)行結(jié)果:

我們將這些數(shù)據(jù)存儲(chǔ)到文件中
程序:

import requests import bs4 import csv url = 'http://www.xinfadi.com.cn/marketanalysis/0/list/1.shtml'fl = open('菜價(jià).csv','w',encoding='utf-8') csvwrite = csv.writer(fl)response = requests.get(url) page_content = response.text #print(page_content) bs_page = bs4.BeautifulSoup(page_content,'html.parser') table = bs_page.find(name='table',attrs={'class':'hq_table'}) trs = table.find_all(name='tr') for tr in trs:tds = tr.find_all(name='td')name = tds[0].textprice_low = tds[1].textprice_ave = tds[2].textprice_high = tds[3].textnorm = tds[4].textunit = tds[5].textdata = tds[6].textcsvwrite.writerow([name,price_low,price_ave,price_high,norm,unit,data])fl.close() print('完成!')

結(jié)果:

0x03 獲取標(biāo)簽的屬性值

頁(yè)面:獲取a標(biāo)簽的href值和name值

<!DOCTYPE html> <html> <head><meta content="text/html;charset=utf-8" http-equiv="content-type" /><meta content="IE=Edge" http-equiv="X-UA-Compatible" /><meta content="always" name="referrer" /><link href="https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css" rel="stylesheet" type="text/css" /><title>百度一下,你就知道 </title> </head> <body link="#0000cc"><div id="wrapper"><div id="head"><div class="head_wrapper"><div id="u1"><a class="mnav" href="http://news.baidu.com" name="tj_trnews">新聞 </a><a class="mnav" href="https://www.hao123.com" name="tj_trhao123">hao123 </a><a class="mnav" href="http://map.baidu.com" name="tj_trmap">地圖 </a><a class="mnav" href="http://v.baidu.com" name="tj_trvideo">視頻 </a><a class="mnav" href="http://tieba.baidu.com" name="tj_trtieba">貼吧 </a><a class="bri" href="//www.baidu.com/more/" name="tj_briicon" style="display: block;">更多產(chǎn)品 </a></div></div></div></div> </body> </html>
  • 獲取標(biāo)簽的屬性,可以再我們獲取標(biāo)簽時(shí),再標(biāo)簽后面加入屬性值,比如說(shuō)a就是我們獲得標(biāo)簽,a[‘href’]就是其鏈接內(nèi)容

程序:

from bs4 import BeautifulSoupwith open('1.html','r',encoding='utf-8') as f:html = f.read() soup = BeautifulSoup(html,'html.parser')print(soup.title)#獲取title標(biāo)簽包含的內(nèi)容 print(soup.title.name)a = soup.find_all(name='a',attrs={'class':'mnav'}) for i in a:print(i['href'],i['name'],sep=' ')

運(yùn)行結(jié)果:

總結(jié)

以上是生活随笔為你收集整理的python安全攻防---爬虫基础---BeautifulSoup解析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。