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

歡迎訪問 生活随笔!

生活随笔

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

python

分析Python7个爬虫小案例(附源码)

發布時間:2023/12/10 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 分析Python7个爬虫小案例(附源码) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

????????本次的7個python爬蟲小案例涉及到了re正則、xpath、beautiful soup、selenium等知識點,非常適合剛入門python爬蟲的小伙伴參考學習。注:若涉及到版權或隱私問題,請及時聯系我刪除即可。

1.使用正則表達式和文件操作爬取并保存“百度貼吧”某帖子全部內容(該帖不少于5頁。

?本次選取的是百度貼吧中的NBA吧中的一篇帖子,帖子標題是“克萊和哈登,誰歷史地位更高”。爬取的目標是帖子里面的回復內容。

源程序和關鍵結果截圖:

import csv import requests import re import timedef main(page):url = f'https://tieba.baidu.com/p/7882177660?pn={page}'headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'}resp = requests.get(url,headers=headers)html = resp.text# 評論內容comments = re.findall('style="display:;"> (.*?)</div>',html)# 評論用戶users = re.findall('class="p_author_name j_user_card" href=".*?" target="_blank">(.*?)</a>',html)# 評論時間comment_times = re.findall('樓</span><span class="tail-info">(.*?)</span><div',html)for u,c,t in zip(users,comments,comment_times):# 篩選數據,過濾掉異常數據if 'img' in c or 'div' in c or len(u)>50:continuecsvwriter.writerow((u,t,c))print(u,t,c)print(f'第{page}頁爬取完畢')if __name__ == '__main__':with open('01.csv','a',encoding='utf-8')as f:csvwriter = csv.writer(f)csvwriter.writerow(('評論用戶','評論時間','評論內容'))for page in range(1,8): # 爬取前7頁的內容main(page)time.sleep(2)

2.實現多線程爬蟲爬取某小說部分章節內容并以數據庫存儲(不少于10個章節。?

?本次選取的小說網址是全本小說網https://www.qb5.tw/,這里我們選取第一篇小說進行爬取

然后通過分析網頁源代碼分析每章小說的鏈接

找到鏈接的位置后,我們使用Xpath來進行鏈接和每一章標題的提取

在這里,因為涉及到多次使用requests發送請求,所以這里我們把它封裝成一個函數,便于后面的使用

每一章的鏈接獲取后,我們開始進入小說章節內容頁面進行分析

通過網頁分析,小說內容都在網頁源代碼中,屬于靜態數據

這里我們選用re正則表達式進行數據提取,并對最后的結果進行清洗

然后我們需要將數據保存到數據庫中,這里我將爬取的數據存儲到mysql數據庫中,先封住一下數據庫的操作

接著將爬取到是數據進行保存

最后一步就是使用多線程來提高爬蟲效率,這里我們創建了5個線程的線程池

?源代碼及結果截圖:

import requests from lxml import etree import re import pymysql from time import sleep from concurrent.futures import ThreadPoolExecutordef get_conn():# 創建連接conn = pymysql.connect(host="127.0.0.1",user="root",password="root",db="novels",charset="utf8")# 創建游標cursor = conn.cursor()return conn, cursordef close_conn(conn, cursor):cursor.close()conn.close()def get_xpath_resp(url):headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'}resp = requests.get(url, headers=headers)tree = etree.HTML(resp.text) # 用etree解析htmlreturn tree,respdef get_chapters(url):tree,_ = get_xpath_resp(url)# 獲取小說名字novel_name = tree.xpath('//*[@id="info"]/h1/text()')[0]# 獲取小說數據節點dds = tree.xpath('/html/body/div[4]/dl/dd')title_list = []link_list = []for d in dds[:15]:title = d.xpath('./a/text()')[0] # 章節標題title_list.append(title)link = d.xpath('./a/@href')[0] # 章節鏈接chapter_url = url +link # 構造完整鏈接link_list.append(chapter_url)return title_list,link_list,novel_namedef get_content(novel_name,title,url):try:cursor = Noneconn = Noneconn, cursor = get_conn()# 插入數據的sqlsql = 'INSERT INTO novel(novel_name,chapter_name,content) VALUES(%s,%s,%s)'tree,resp = get_xpath_resp(url)# 獲取內容content = re.findall('<div id="content">(.*?)</div>',resp.text)[0]# 對內容進行清洗content = content.replace('<br />','\n').replace('&nbsp;',' ').replace('全本小說網 www.qb5.tw,最快更新<a href="https://www.qb5.tw/book_116659/">宇宙職業選手</a>最新章節!<br><br>','')print(title,content)cursor.execute(sql,[novel_name,title,content]) # 插入數據conn.commit() # 提交事務保存數據except:passfinally:sleep(2)close_conn(conn, cursor) # 關閉數據庫if __name__ == '__main__':# 獲取小說名字,標題鏈接,章節名稱title_list, link_list, novel_name = get_chapters('https://www.qb5.tw/book_116659/')with ThreadPoolExecutor(5) as t: # 創建5個線程for title,link in zip(title_list,link_list):t.submit(get_content, novel_name,title,link) # 啟動線程

?3. 分別使用XPath和Beautiful Soup4兩種方式爬取并保存非異步加載的“豆瓣某排行榜”如https://movie.douban.com/top250的名稱、描述、評分和評價人數等數據。

?先分析:

首先,來到豆瓣Top250頁面,首先使用Xpath版本的來抓取數據,先分析下電影列表頁的數據結構,發下都在網頁源代碼中,屬于靜態數據

接著我們找到數據的規律,使用xpath提取每一個電影的鏈接及電影名

然后根據鏈接進入到其詳情頁

分析詳情頁的數據,發現也是靜態數據,繼續使用xpath提取數據

最后我們將爬取的數據進行存儲,這里用csv文件進行存儲

接著是Beautiful Soup4版的,在這里,我們直接在電影列表頁使用bs4中的etree進行數據提取

最后,同樣使用csv文件進行數據存儲

源代碼即結果截圖:

XPath版:

import re from time import sleep import requests from lxml import etree import random import csvdef main(page,f):url = f'https://movie.douban.com/top250?start={page*25}&filter='headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.35 Safari/537.36',}resp = requests.get(url,headers=headers)tree = etree.HTML(resp.text)# 獲取詳情頁的鏈接列表href_list = tree.xpath('//*[@id="content"]/div/div[1]/ol/li/div/div[1]/a/@href')# 獲取電影名稱列表name_list = tree.xpath('//*[@id="content"]/div/div[1]/ol/li/div/div[2]/div[1]/a/span[1]/text()')for url,name in zip(href_list,name_list):f.flush() # 刷新文件try:get_info(url,name) # 獲取詳情頁的信息except:passsleep(1 + random.random()) # 休息print(f'第{i+1}頁爬取完畢')def get_info(url,name):headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.35 Safari/537.36','Host': 'movie.douban.com',}resp = requests.get(url,headers=headers)html = resp.texttree = etree.HTML(html)# 導演dir = tree.xpath('//*[@id="info"]/span[1]/span[2]/a/text()')[0]# 電影類型type_ = re.findall(r'property="v:genre">(.*?)</span>',html)type_ = '/'.join(type_)# 國家country = re.findall(r'地區:</span> (.*?)<br',html)[0]# 上映時間time = tree.xpath('//*[@id="content"]/h1/span[2]/text()')[0]time = time[1:5]# 評分rate = tree.xpath('//*[@id="interest_sectl"]/div[1]/div[2]/strong/text()')[0]# 評論人數people = tree.xpath('//*[@id="interest_sectl"]/div[1]/div[2]/div/div[2]/a/span/text()')[0]print(name,dir,type_,country,time,rate,people) # 打印結果csvwriter.writerow((name,dir,type_,country,time,rate,people)) # 保存到文件中if __name__ == '__main__':# 創建文件用于保存數據with open('03-movie-xpath.csv','a',encoding='utf-8',newline='')as f:csvwriter = csv.writer(f)# 寫入表頭標題csvwriter.writerow(('電影名稱','導演','電影類型','國家','上映年份','評分','評論人數'))for i in range(10): # 爬取10頁main(i,f) # 調用主函數sleep(3 + random.random())

Beautiful Soup4版:?

import random import urllib.request from bs4 import BeautifulSoup import codecs from time import sleepdef main(url, headers):# 發送請求page = urllib.request.Request(url, headers=headers)page = urllib.request.urlopen(page)contents = page.read()# 用BeautifulSoup解析網頁soup = BeautifulSoup(contents, "html.parser")infofile.write("")print('爬取豆瓣電影250: \n')for tag in soup.find_all(attrs={"class": "item"}):# 爬取序號num = tag.find('em').get_text()print(num)infofile.write(num + "\r\n")# 電影名稱name = tag.find_all(attrs={"class": "title"})zwname = name[0].get_text()print('[中文名稱]', zwname)infofile.write("[中文名稱]" + zwname + "\r\n")# 網頁鏈接url_movie = tag.find(attrs={"class": "hd"}).aurls = url_movie.attrs['href']print('[網頁鏈接]', urls)infofile.write("[網頁鏈接]" + urls + "\r\n")# 爬取評分和評論數info = tag.find(attrs={"class": "star"}).get_text()info = info.replace('\n', ' ')info = info.lstrip()print('[評分評論]', info)# 獲取評語info = tag.find(attrs={"class": "inq"})if (info): # 避免沒有影評調用get_text()報錯content = info.get_text()print('[影評]', content)infofile.write(u"[影評]" + content + "\r\n")print('')if __name__ == '__main__':# 存儲文件infofile = codecs.open("03-movie-bs4.txt", 'a', 'utf-8')# 消息頭headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'}# 翻頁i = 0while i < 10:print('頁碼', (i + 1))num = i * 25 # 每次顯示25部 URL序號按25增加url = 'https://movie.douban.com/top250?start=' + str(num) + '&filter='main(url, headers)sleep(5 + random.random())infofile.write("\r\n\r\n")i = i + 1infofile.close()

?

?4.實現某東商城某商品評論數據的爬取(評論數據不少于100條,包括評論內容、時間和評分)。

?先分析:

?本次選取的某東官網的一款聯想筆記本電腦,數據為動態加載的,通過開發者工具抓包分析即可。

源代碼及結果截圖:

import requests import csv from time import sleep import randomdef main(page,f):url = 'https://club.jd.com/comment/productPageComments.action'params = {'productId': 100011483893,'score': 0,'sortType': 5,'page': page,'pageSize': 10,'isShadowSku': 0,'fold': 1}headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.35 Safari/537.36','referer': 'https://item.jd.com/'}resp = requests.get(url,params=params,headers=headers).json()comments = resp['comments']for comment in comments:content = comment['content']content = content.replace('\n','')comment_time = comment['creationTime']score = comment['score']print(score,comment_time,content)csvwriter.writerow((score,comment_time,content))print(f'第{page+1}頁爬取完畢')if __name__ == '__main__':with open('04.csv','a',encoding='utf-8',newline='')as f:csvwriter = csv.writer(f)csvwriter.writerow(('評分','評論時間','評論內容'))for page in range(15):main(page,f)sleep(5+random.random())

5. 實現多種方法模擬登錄知乎,并爬取與一個與江漢大學有關問題和答案。

首先使用selenium打開知乎登錄頁面,接著使用手機進行二維碼掃描登錄

進入頁面后,打開開發者工具,找到元素,,定位輸入框,輸入漢江大學,然后點擊搜索按鈕

?

以第二條帖子為例,進行元素分析?。

源代碼及結果截圖:

from time import sleep from selenium.webdriver.chrome.service import Service from selenium.webdriver import Chrome,ChromeOptions from selenium.webdriver.common.by import By import warningsdef main():#忽略警告warnings.filterwarnings("ignore")# 創建一個驅動service = Service('chromedriver.exe')options = ChromeOptions()# 偽造瀏覽器options.add_experimental_option('excludeSwitches', ['enable-automation','enable-logging'])options.add_experimental_option('useAutomationExtension', False)# 創建一個瀏覽器driver = Chrome(service=service,options=options)# 繞過檢測driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {"source": """Object.defineProperty(navigator, 'webdriver', {get: () => false})"""})# 打開知乎登錄頁面driver.get('https://www.zhihu.com/')sleep(30)# 點擊搜索框driver.find_element(By.ID,'Popover1-toggle').click()# 輸入內容driver.find_element(By.ID,'Popover1-toggle').send_keys('漢江大學')sleep(2)# 點擊搜索圖標driver.find_element(By.XPATH,'//*[@id="root"]/div/div[2]/header/div[2]/div[1]/div/form/div/div/label/button').click()# 等待頁面加載完driver.implicitly_wait(20)# 獲取標題title = driver.find_element(By.XPATH,'//*[@id="SearchMain"]/div/div/div/div/div[2]/div/div/div/h2/div/a/span').text# 點擊閱讀全文driver.find_element(By.XPATH,'//*[@id="SearchMain"]/div/div/div/div/div[2]/div/div/div/div/span/div/button').click()sleep(2)# 獲取帖子內容content = driver.find_element(By.XPATH,'//*[@id="SearchMain"]/div/div/div/div/div[2]/div/div/div/div/span[1]/div/span/p').text# 點擊評論driver.find_element(By.XPATH,'//*[@id="SearchMain"]/div/div/div/div/div[2]/div/div/div/div/div[3]/div/div/button[1]').click()sleep(2)# 點擊獲取更多評論driver.find_element(By.XPATH,'//*[@id="SearchMain"]/div/div/div/div/div[2]/div/div/div/div[2]/div/div/div[2]/div[2]/div/div[3]/button').click()sleep(2)# 獲取評論數據的節點divs = driver.find_elements(By.XPATH,'/html/body/div[6]/div/div/div[2]/div/div/div/div[2]/div[3]/div')try:for div in divs:# 評論內容comment = div.find_element(By.XPATH,'./div/div/div[2]').textf.write(comment) # 寫入文件f.write('\n')print(comment)except:driver.close()if __name__ == '__main__':# 創建文件存儲數據with open('05.txt','a',encoding='utf-8')as f:main()

?6. 綜合利用所學知識,爬取某個某博用戶前5頁的微博內容。

這里我們選取了人民日報的微博內容進行爬取,具體頁面我就不放這了,怕違規。

源代碼及結果截圖:

import requests import csv from time import sleep import randomdef main(page):url = f'https://weibo.com/ajax/statuses/mymblog?uid=2803301701&page={page}&feature=0&since_id=4824543023860882kp{page}'headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36','cookie':'SINAGLOBAL=6330339198688.262.1661412257300; ULV=1661412257303:1:1:1:6330339198688.262.1661412257300:; PC_TOKEN=8b935a3a6e; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WWoQDW1G.Vsux_WIbm9NsCq5JpX5KMhUgL.FoMNShMN1K5ESKq2dJLoIpjLxKnL1h.LB.-LxKqLBoBLB.-LxKqLBKeLB--t; ALF=1697345086; SSOLoginState=1665809086; SCF=Auy-TaGDNaCT06C4RU3M3kQ0-QgmTXuo9D79pM7HVAjce1K3W92R1-fHAP3gXR6orrHK_FSwDsodoGTj7nX_1Hw.; SUB=_2A25OTkruDeRhGeFJ71UW-S7OzjqIHXVtOjsmrDV8PUNbmtANLVKmkW9Nf9yGtaKedmyOsDKGh84ivtfHMGwvRNtZ; XSRF-TOKEN=LK4bhZJ7sEohF6dtSwhZnTS4; WBPSESS=PfYjpkhjwcpEXrS7xtxJwmpyQoHWuGAMhQkKHvr_seQNjwPPx0HJgSgqWTZiNRgDxypgeqzSMsbVyaDvo7ng6uTdC9Brt07zYoh6wXXhQjMtzAXot-tZzLRlW_69Am82CXWOFfcvM4AzsWlAI-6ZNA=='}resp = requests.get(url,headers=headers)data_list = resp.json()['data']['list']for item in data_list:created_time = item['created_at'] # 發布時間author = item['user']['screen_name'] # 作者title = item['text_raw'] # 帖子標題reposts_count = item['reposts_count'] # 轉發數comments_count = item['comments_count'] # 評論數attitudes_count = item['attitudes_count'] # 點贊數csvwriter.writerow((created_time,author,title,reposts_count,comments_count,attitudes_count))print(created_time,author,title,reposts_count,comments_count,attitudes_count)print(f'第{page}頁爬取完畢')if __name__ == '__main__':# 創建保存數據的csv文件with open('06-2.csv','a',encoding='utf-8',newline='')as f:csvwriter = csv.writer(f)# 添加文件表頭csvwriter.writerow(('發布時間','發布作者','帖子標題','轉發數','評論數','點贊數'))for page in range(1,6): # 爬取前5頁數據main(page)sleep(5+random.random())

?7.自選一個熱點或者你感興趣的主題,爬取數據并進行簡要數據分析(例如,通過爬取電影的名稱、類型、總票房等數據統計分析不同類型電影的平均票房,十年間每年票房冠軍的票房走勢等;通過爬取中國各省份地區人口數量,統計分析我國人口分布等)。

本次選取的網址是藝恩娛數,目標是爬取里面的票房榜數據,通過開發者工具抓包分析找到數據接口,然后開始編寫代碼進行數據抓取。?

源代碼及結果截圖:

import requests import csv import pandas as pd import matplotlib.pyplot as plt import warnings warnings.filterwarnings('ignore') plt.rcParams['font.sans-serif'] = ['SimHei'] #解決中文顯示 plt.rcParams['axes.unicode_minus'] = False #解決符號無法顯示def main():headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36',}data = {'r': '0.9936776079863086','top': '50','type': '0',}resp = requests.post('https://ys.endata.cn/enlib-api/api/home/getrank_mainland.do', headers=headers, data=data)data_list = resp.json()['data']['table0']for item in data_list:rank = item['Irank'] # 排名MovieName = item['MovieName'] # 電影名稱ReleaseTime = item['ReleaseTime'] # 上映時間TotalPrice = item['BoxOffice'] # 總票房(萬)AvgPrice = item['AvgBoxOffice'] # 平均票價AvgAudienceCount = item['AvgAudienceCount'] # 平均場次# 寫入csv文件csvwriter.writerow((rank,MovieName,ReleaseTime,TotalPrice,AvgPrice,AvgAudienceCount))print(rank,MovieName,ReleaseTime,TotalPrice,AvgPrice,AvgAudienceCount)def data_analyze():# 讀取數據data = pd.read_csv('07.csv')# 從上映時間中提取出年份data['年份'] = data['上映時間'].apply(lambda x: x.split('-')[0])# 各年度上榜電影總票房占比df1 = data.groupby('年份')['總票房(萬)'].sum()plt.figure(figsize=(6, 6))plt.pie(df1, labels=df1.index.to_list(), autopct='%1.2f%%')plt.title('各年度上榜電影總票房占比')plt.show()# 各個年份總票房趨勢df1 = data.groupby('年份')['總票房(萬)'].sum()plt.figure(figsize=(6, 6))plt.plot(df1.index.to_list(), df1.values.tolist())plt.title('各年度上榜電影總票房趨勢')plt.show()# 平均票價最貴的前十名電影print(data.sort_values(by='平均票價', ascending=False)[['年份', '電影名稱', '平均票價']].head(10))# 平均場次最高的前十名電影print(data.sort_values(by='平均場次', ascending=False)[['年份', '電影名稱', '平均場次']].head(10))if __name__ == '__main__':# 創建保存數據的csv文件with open('07.csv', 'w', encoding='utf-8',newline='') as f:csvwriter = csv.writer(f)# 添加文件表頭csvwriter.writerow(('排名', '電影名稱', '上映時間', '總票房(萬)', '平均票價', '平均場次'))main()# 數據分析data_analyze()

?

?從年度上榜電影票房占比來看,2019年占比最高,說明2019年這一年的電影質量都很不錯,上榜電影多而且票房高。

從趨勢來看,從2016年到2019年,上榜電影總票房一直在增長,到2019年達到頂峰,說明這一年電影是非常的火爆,但是從2020年急劇下滑,最大的原因應該是這一年年初開始爆發疫情,導致賀歲檔未初期上映,而且由于疫情影響,電影院一直處于關閉狀態,所以這一年票房慘淡。

????????好了,本次案例分享到此結束,希望對剛入手爬蟲的小伙伴有所幫助。?

總結

以上是生活随笔為你收集整理的分析Python7个爬虫小案例(附源码)的全部內容,希望文章能夠幫你解決所遇到的問題。

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