利用python的爬蟲技術實現網頁融合!

2021-10-25 12:13:49 字數 920 閱讀 2801

第二個思路使用爬蟲技術也就是本次使用的方法。

from bs4 import beautifulsoup
要使用bs4這個庫,這個庫很好安裝,當然還要安裝乙個依賴庫。

直接上**了,注釋寫的很清楚

from collections import defaultdict

from bs4 import beautifulsoup

def mergehtml(path):

allplotfile = open(path + '\\data\\allplot.html', 'rb')

htmla = allplotfile.read()

bsa = beautifulsoup(htmla,"html.parser")

plotstatusfile = open(path + '\\data\\plotstatus.html', 'rb')

htmlp = plotstatusfile.read()

bsp = beautifulsoup(htmlp,"html.parser")

bsahead = bsa.head

bsabody = bsa.body

bsp.head.decompose() #清除head標籤

bsp.html.insert_before(bsahead) #插入bsahead標籤

bsp.html.insert_before(bsabody) #插入bsabody標籤

with open(path + '\\data\\all.html','w') as f:

f.write(bsp.prettify()) #把合成的內容寫入html中

if __name__ == "__main__":

mergehtml('.\\path')

建議讀官網,有中文例子:

python爬蟲六 反爬蟲技術種類

zhuanlan.zhihu.com referer ehco 知乎 很多 都會建立 user agent白名單,只有屬於正常範圍的user agent才能夠正常訪問。模擬header的user agent欄位,返回乙個隨機的user agent字典型別的鍵值對 agents mozilla 5.0...

python爬蟲入門(四)利用多執行緒爬蟲

先回顧前面學過的一些知識 1.乙個cpu一次只能執行乙個任務,多個cpu同時可以執行多個任務 2.乙個cpu一次只能執行乙個程序,其它程序處於非執行狀態 3.程序裡包含的執行單元叫執行緒,乙個程序可以包含多個執行緒 4.乙個程序的記憶體空間是共享的,每個程序裡的執行緒都可以使用這個共享空間 5.乙個...

python爬蟲基礎實現 Python實現基礎爬蟲

初次使用urllib實現爬蟲的資料請求 urllib.request.urlopen url 發起get請求 urllib.parse.quote 將中文進行url編碼 from urllib.request importurlopen,urlretrieve,requestfrom urllib....