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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

腾讯爬虫python_Python爬虫,爬取腾讯漫画实战

發(fā)布時間:2024/10/12 python 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 腾讯爬虫python_Python爬虫,爬取腾讯漫画实战 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

先上個爬取的結(jié)果圖

最后的結(jié)果為每部漫畫按章節(jié)保存

運行環(huán)境

IDE VS2019

Python3.7

先上代碼,代碼非常簡短,包含空行也才50行,多虧了python強大的庫

importosimporttimeimportrequestsfrom selenium importwebdriverfrom lxml importetreedefgetChapterUrl(url):

headers={"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36"}

part_url= "http://ac.qq.com"res= requests.get(url, headers=headers)

html=res.content.decode()

el=etree.HTML(html)

li_list= el.xpath('//*[@id="chapter"]/div[2]/ol[1]/li')for li inli_list:for p in li.xpath("./p"):for span in p.xpath("./span[@class='works-chapter-item']"):

item={}

list_title= span.xpath("./a/@title")[0].replace(' ', '').split(':')if list_title[1].startswith(('第', '序')):

getChapterFile(part_url+ span.xpath("./a/@href")[0], list_title[0],list_title[1])defgetChapterFile(url,path1,path2):#path = os.path.join(path)

#漫畫名稱目錄

path=os.path.join(path1)if notos.path.exists(path):

os.mkdir(path)#章節(jié)目錄

path=path+'\\'+path2if notos.path.exists(path):

os.mkdir(path)

chrome=webdriver.Chrome()#"http://ac.qq.com/ComicView/index/id/505435/cid/2"

chrome.get(url)

time.sleep(4)

imgs= chrome.find_elements_by_xpath("//div[@id='mainView']/ul[@id='comicContain']//img")for i inrange(0, len(imgs)):

js="document.getElementById('mainView').scrollTop="+str((i) * 1280)

chrome.execute_script(js)

time.sleep(3)print(imgs[i].get_attribute("src"))

with open(path+'\\'+str(i)+'.png', 'wb') as f:

f.write(requests.get(imgs[i].get_attribute("src")).content)

chrome.close()print('下載完成')if __name__ == '__main__':

getChapterUrl('http://ac.qq.com/Comic/ComicInfo/id/505435')

簡單解釋

輸入一個漫畫的url即可爬取該漫畫所有的章節(jié),由于是模擬用戶爬取的,所以速度方面有點慢,我試了下爬取銀魂前70章,用了1個半小時,代碼中的sleep可以適當簡短點已加快爬取的速度

付費的漫畫是沒有辦法爬取的

談一下過程中遇到的坑

騰訊的漫畫網(wǎng)站打開章節(jié)時沒有把所有圖片的url都加載出來,所以我在這里用的方式是使用selenium來模擬用戶操作,每次打開頁面以后使用js操作滾動條下拉

最后再貼下代碼庫,其實貼出的代碼已經(jīng)是所有的代碼了

https://dev.azure.com/shenjuncaci/PythonTecentManhua

總結(jié)

以上是生活随笔為你收集整理的腾讯爬虫python_Python爬虫,爬取腾讯漫画实战的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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