溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python爬取網(wǎng)頁轉(zhuǎn)換為PDF文件

發(fā)布時間:2020-09-11 14:21:30 來源:腳本之家 閱讀:236 作者:moluchase 欄目:開發(fā)技術(shù)

爬蟲的起因

官方文檔或手冊雖然可以查閱,但是如果變成紙質(zhì)版的豈不是更容易翻閱與記憶。如果簡單的復(fù)制粘貼,不知道何時能夠完成。于是便開始想著將Android的官方手冊爬下來。

全篇的實現(xiàn)思路

  1. 分析網(wǎng)頁
  2. 學(xué)會使用BeautifulSoup庫
  3. 爬取并導(dǎo)出

參考資料:

* 把廖雪峰的教程轉(zhuǎn)換為PDF電子書
* Requests文檔
* Beautiful Soup文檔

配置

在Ubuntu下使用Pycharm運行成功
轉(zhuǎn)PDF需要下載wkhtmltopdf

具體過程

網(wǎng)頁分析

如下所示的一個網(wǎng)頁,要做的是獲取該網(wǎng)頁的正文和標(biāo)題,以及左邊導(dǎo)航條的所有網(wǎng)址

python爬取網(wǎng)頁轉(zhuǎn)換為PDF文件

接下來的工作就是找到這些標(biāo)簽嘍…

關(guān)于Requests的使用

詳細(xì)參考文檔,這里只是簡單的使用Requests獲取html以及使用代理翻墻(網(wǎng)站無法直接訪問,需要VPN)

proxies={
 "http":"http://vpn的IP:port",
 "https":"https://vpn的IP:port",
 }

response=requests.get(url,proxies=proxies)

Beautiful Soup的使用

參考資料里面有Beautiful Soup文檔,將其看完后,可以知道就講了兩件事:一個是查找標(biāo)簽,一個是修改標(biāo)簽。
本文需要做的是:

1. 獲取標(biāo)題和所有的網(wǎng)址,涉及到的是查找標(biāo)簽

#對標(biāo)簽進行判斷,一個標(biāo)簽含有href而不含有description,則返回true
#而我希望獲取的是含有href屬性而不含有description屬性的<a>標(biāo)簽,(且只有a標(biāo)簽含有href)
def has_href_but_no_des(tag):
 return tag.has_attr('href') and not tag.has_attr('description')

#網(wǎng)頁分析,獲取網(wǎng)址和標(biāo)題
def parse_url_to_html(url):

 response=requests.get(url,proxies=proxies)
 soup=BeautifulSoup(response.content,"html.parser")
 s=[]#獲取所有的網(wǎng)址
 title=[]#獲取對應(yīng)的標(biāo)題
 tag=soup.find(id="nav")#獲取第一個id為"nav"的標(biāo)簽,這個里面包含了網(wǎng)址和標(biāo)題
 for i in tag.find_all(has_href_but_no_des):
 s.append(i['href'])
 title.append(i.text)

 #獲取的只是標(biāo)簽集,需要加html前綴
 htmls = "<html><head><meta charset='UTF-8'></head><body>"
 with open("android_training_3.html",'a') as f:
 f.write(htmls)

對上面獲取的網(wǎng)址分析,獲取正文,并將圖片取出存于本地;涉及到的是查找標(biāo)簽和修改屬性

#網(wǎng)頁操作,獲取正文及圖片
def get_htmls(urls,title):

 for i in range(len(urls)):
 response=requests.get(urls[i],proxies=proxies)
 soup=BeautifulSoup(response.content,"html.parser")
 htmls="<div><h2>"+str(i)+"."+title[i]+"</h2></div>"
 tag=soup.find(class_='jd-descr')
 #為image添加相對路徑,并下載圖片
 for img in tag.find_all('img'):
  im = requests.get(img['src'], proxies=proxies)
  filename = os.path.split(img['src'])[1]
  with open('image/' + filename, 'wb') as f:
  f.write(im.content)
  img['src']='image/'+filename
 htmls=htmls+str(tag)
 with open("android_training_3.html",'a') as f:
  f.write(htmls)
 print(" (%s) [%s] download end"%(i,title[i]))
 htmls="</body></html>"
 with open("android_training_3.html",'a') as f:
 f.write(htmls)

2.轉(zhuǎn)為PDF

這一步需要下載wkhtmltopdf,在Windows下執(zhí)行程序一直出錯..Ubuntu下可以

def save_pdf(html):
 """
 把所有html文件轉(zhuǎn)換成pdf文件
 """
 options = {
 'page-size': 'Letter',
 'encoding': "UTF-8",
 'custom-header': [
  ('Accept-Encoding', 'gzip')
 ]
 }
 pdfkit.from_file(html, "android_training_3.pdf", options=options)

最后的效果圖

python爬取網(wǎng)頁轉(zhuǎn)換為PDF文件

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI