[問題] 爬取圖文成檔案
爬文都只有只爬取圖片或文字
用requests和bs4爬取文字或圖片是會了
但是想要爬取成圖文混排
所以txt是沒辦法了
目前能夠圖文混排的格式打算用world或epub
但是不知道要怎麼爬取圖文
用bs4只會跑出圖片的連結
沒有頭緒了...
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.224.161.174 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1577204893.A.478.html
→
12/25 00:59,
4年前
, 1F
12/25 00:59, 1F
→
12/25 01:00,
4年前
, 2F
12/25 01:00, 2F
→
12/25 01:01,
4年前
, 3F
12/25 01:01, 3F
→
12/25 01:02,
4年前
, 4F
12/25 01:02, 4F
我又不是很常用這功能...
雖然是比兩年前會很多
不過爬蟲是最近才嘗試的東西
找到的答案又都是單純爬圖或爬文
html...
嘗試還是只能爬圖或文
寫不出一起爬的
→
12/25 02:35,
4年前
, 5F
12/25 02:35, 5F
這是?
輕量級標記式語言?
※ 編輯: s4028600 (125.224.161.174 臺灣), 12/25/2019 05:59:01
→
12/25 09:36,
4年前
, 6F
12/25 09:36, 6F
→
12/25 09:38,
4年前
, 7F
12/25 09:38, 7F
→
12/25 09:40,
4年前
, 8F
12/25 09:40, 8F
import requests
from bs4 import BeautifulSoup
import os
url = 'https://ericjhang.github.io/archives/ad5450f3.html'
html = requests.get(url).content
with open('123.html','wb')as f:
f.write(html)
f.close()
這樣爬出來圖是叉叉
怎麼鑲嵌比較好
※ 編輯: s4028600 (125.224.161.174 臺灣), 12/25/2019 11:08:54
→
12/25 12:40,
4年前
, 9F
12/25 12:40, 9F
→
12/25 12:41,
4年前
, 10F
12/25 12:41, 10F
→
12/25 12:41,
4年前
, 11F
12/25 12:41, 11F
→
12/25 12:42,
4年前
, 12F
12/25 12:42, 12F
兩年前也才一篇文章
而且還是文件處理
中間並沒有用過好嗎?
而且就是不懂才要問
就算聽不懂也可以抽絲剝繭從回答中找到更多東西
你的html建議就很有用
的確我還摸索不出來
不過比你指責我英文不好要有用多了
※ 編輯: s4028600 (175.183.44.67 臺灣), 12/25/2019 14:14:52
https://blog.csdn.net/he_string/article/details/78574198
根據這篇文章可以把圖放入world
可是只能放到最後面...
※ 編輯: s4028600 (175.183.44.67 臺灣), 12/25/2019 14:18:34
→
12/25 16:54,
4年前
, 13F
12/25 16:54, 13F
→
12/25 19:00,
4年前
, 14F
12/25 19:00, 14F
→
12/25 19:29,
4年前
, 15F
12/25 19:29, 15F
→
12/25 19:30,
4年前
, 16F
12/25 19:30, 16F
→
12/25 19:31,
4年前
, 17F
12/25 19:31, 17F
→
12/25 19:31,
4年前
, 18F
12/25 19:31, 18F
有想過也試過喔
最主要是我還做不到批量改html裡的連結
所以才難以合併
不過剛找到方法了 還很簡陋就是了
→
12/25 19:46,
4年前
, 19F
12/25 19:46, 19F
我的確還是copy paste的程度沒錯
不過比以前只能直接用 現在會自己改了
雖然進度緩慢...
→
12/26 00:27,
4年前
, 20F
12/26 00:27, 20F
好吧
研究epub的結構後覺得我的確在一步登天
還是從Hsins的建議從html開始
不過我的確不太會從細部問題開始
多是大問題開始走一步算一步...
圖已經會載了
→
12/26 00:28,
4年前
, 21F
12/26 00:28, 21F
是指在print之前的排版嗎
為什麼你們留言我app都沒顯示...
總之找到一個算可行方法
但是很仰賴calibre
import zipfile
import requests
from bs4 import BeautifulSoup
a=1
url = ''
res=requests.get(url)
res.encoding='gbk'
soup=BeautifulSoup(res.text,'html.parser')
html=soup.select('#contentmain')[0].prettify()
outZip=zipfile.ZipFile('test.zip', mode='w', compression=zipfile.ZIP_DEFLATED)
#, compresslevel=9)
for img in soup.select('#contentmain'):
for src in img.select('img'):
filename='images/%02d.jpg'%a
print(src['src'])
html=html.replace(src['src'],filename)
imgUrl=src['src']
imgResponse=requests.get(imgUrl)
outZip.writestr(filename, imgResponse.content)
a+=1
print(html)
htmlContent=html
outZip.writestr('index.html', htmlContent)
outZip.close()
然後再用calibre轉檔
※ 編輯: s4028600 (36.232.106.188 臺灣), 12/26/2019 13:47:45
→
12/26 14:57,
4年前
, 22F
12/26 14:57, 22F