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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

爬取虎扑NBA首页主干道推荐贴的一只小爬虫,日常爬不冷笑话解闷

發布時間:2024/10/12 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 爬取虎扑NBA首页主干道推荐贴的一只小爬虫,日常爬不冷笑话解闷 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

虎撲是廣大jrs的家園,步行街是這個家園里最繁華的地段。據稱廣大jrs平均學歷985,步行街街薪30w起步。

大學時經舍友安利,開始了解虎撲,主要是看看NBA的一些資訊。

偶爾也上上這個破街,看看jrs虐虐狗,說說家長里短等等,別的不說,jr們的三觀都是特別正的。

不冷笑話基本是我每天必看的帖子,感覺樓主非常敬業,每天都會有高質量的輸出,帖子下的熱帖也很給力,福利滿滿。

正學python,突發奇想想把不冷笑話的圖都爬下來。

但是虎撲在這塊有限制,不登錄無法查看用戶的帖子,而我目前又懶得弄登陸認證(主要是還沒學通-_-||)。

經過長期的觀察驗證,我發現不冷笑話每次都在首頁主干道的固定位置,于是萌生出了直接從首頁定位到帖子里的想法。

?

說干就干,經過我的一通分析,終于把程序寫好了,爬蟲的工作流程如下:

1、定位不冷笑話在首頁的位置,獲取鏈接和標題

2、建立以標題命名的目錄,如果目錄存在,說明已下載,程序結束

3、進入不冷笑話的界面,獲取正文中的圖片鏈接,存入列表

4、獲取亮貼中的圖片鏈接,存入列表

5、保存圖片,根據傳入參數為正文或評論進行命名,區分圖片來源

6、大功告成

?

#-*- coding: utf-8 -*- import requests from bs4 import BeautifulSoup import os, time import re url = (r'https://nba.hupu.com/')#獲取不冷笑話在首頁的位置,返回url和標題 def get_buleng_title_url(url):index_html = requests.get(url)index_html_s = BeautifulSoup(index_html.text,'lxml')main_street = index_html_s.find(class_ = 'gray-list main-stem max250')url_list = []url_name_list = []for dd in main_street.find_all('dd',limit = 5):url_list.append(dd.a.get('href'))url_name_list.append(dd.a.get_text())return [url_list[4],url_name_list[4]] #獲取不冷笑話正文中的圖片列表,利用set去重 def get_pic_url(buleng_list):pic_url_list = set()buleng_html = requests.get(buleng_list[0])buleng_html_s = BeautifulSoup(buleng_html.text,'lxml')buleng_content = buleng_html_s.find(class_='quote-content')for pic_url in buleng_content.find_all('img'):try:original_url = pic_url.get('data-original')pic_url_list.add(original_url.split('?')[0])except:pic_url_list.add(pic_url.get('src'))return pic_url_list#創建以標題命名的文件夾,并返回是否創建成功 def makedir(buleng_list):path = ('E:\\pic\\%s' % buleng_list[1])if os.path.exists(path):return 0else:os.makedirs(path)return path#獲取亮貼中的圖片列表,set去重def get_comment_pic_url(buleng_list):comment_pic_url_list = set()buleng_html = requests.get(buleng_list[0])buleng_html_s = BeautifulSoup(buleng_html.text,'lxml')buleng_comment = buleng_html_s.find(id='readfloor')for floor in buleng_comment.find_all('table'):for pic_url in floor.find_all('img'): try:original_url = pic_url.get('data-original')comment_pic_url_list.add(original_url.split('?')[0])except:comment_pic_url_list.add(pic_url.get('src'))return comment_pic_url_list#下載圖片,可下載gif、jpg、png格式 def download_pic(pic_url_list,path,pic_from = '正文'):a = 1for url in pic_url_list :if url.endswith('.gif'):pic = requests.get(url)with open((path+('\\%s-%s.gif' % (pic_from,a))),'wb') as f:f.write(pic.content)f.closeprint('下載一張%s動圖' % pic_from)a += 1if url.endswith('.jpg'):pic = requests.get(url)with open((path+('\\%s-%s.jpg' % (pic_from,a))),'wb') as f:f.write(pic.content)f.closeprint('下載一張%sjpg圖' % pic_from)a +=1if url.endswith('.png'):pic = requests.get(url)with open((path+('\\%s-%s.png' % (pic_from,a))),'wb') as f:f.write(pic.content)f.closeprint('下載一張%spng圖' % pic_from)a +=1if __name__ == "__main__":buleng = get_buleng_title_url(url)path = makedir(buleng)if path != 0:pic_url_list = get_pic_url(buleng)comment_pic_url_list = get_comment_pic_url(buleng)download_pic(pic_url_list,path)download_pic(comment_pic_url_list,path,'評論')else:print('目錄已存在,等待虎撲更新')

?

總結:

這個程序的主要判定貼子位置的辦法就是首頁帖子順序,稍微修改一下也可以爬取主干道的其他推薦熱帖,代碼就不放了。

補充:

我已經把寫的爬取推薦熱帖的代碼,放到了GitHub上,還沒太搞懂Git的用法,大家多指教

地址:https://github.com/mathdogggg/zhugandao

?

轉載于:https://www.cnblogs.com/mathbox/p/9182388.html

總結

以上是生活随笔為你收集整理的爬取虎扑NBA首页主干道推荐贴的一只小爬虫,日常爬不冷笑话解闷的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。