<kbd id="9plqc"><label id="9plqc"></label></kbd>

        <th id="9plqc"></th>
        1. <center id="9plqc"><video id="9plqc"></video></center>
          <sub id="9plqc"><form id="9plqc"><pre id="9plqc"></pre></form></sub>
          <nav id="9plqc"><form id="9plqc"><legend id="9plqc"></legend></form></nav>
          Python網(wǎng)站搜索內(nèi)容定向爬蟲(新手向超詳細(xì)) 您所在的位置:網(wǎng)站首頁 屬龍和屬鼠的母女合不合 Python網(wǎng)站搜索內(nèi)容定向爬蟲(新手向超詳細(xì))

          Python網(wǎng)站搜索內(nèi)容定向爬蟲(新手向超詳細(xì))

          #Python網(wǎng)站搜索內(nèi)容定向爬蟲(新手向超詳細(xì))| 來源: 網(wǎng)絡(luò)整理| 查看: 265

          目錄 功能前期準(zhǔn)備各個(gè)模塊功能代碼部分代碼解析getHtmlparsePageprintlist 運(yùn)行效果總結(jié)

          功能 目標(biāo)網(wǎng)站:https://www.hellohuanxuan/定向爬蟲,只能爬取給定URL,不進(jìn)行擴(kuò)展爬取爬蟲向搜索框提交搜索信息,爬取搜索之后的結(jié)果所需庫:requests,bs4 前期準(zhǔn)備 首先查看網(wǎng)頁搜索框,隨便搜索數(shù)據(jù)看看 在這里插入圖片描述 我們注意到,此時(shí)url為:file 可推斷出執(zhí)行搜索的參數(shù)為 “?s=”之后打開F12查看源代碼,看到整個(gè)數(shù)據(jù)部分是在一個(gè)main標(biāo)簽里,如發(fā)表時(shí)間、標(biāo)題,鏈接等等 在這里插入圖片描述開始構(gòu)造代碼吧 各個(gè)模塊功能 整個(gè)爬蟲分為三大模塊,每個(gè)模塊一個(gè)函數(shù)getHtml(url, header)函數(shù):發(fā)起請求,獲得返回源代碼parsePage(ulist, html)函數(shù):負(fù)責(zé)解析源代碼,獲取到有用的信息,并存入列表中(整個(gè)代碼的關(guān)鍵部分)printlist(ulist)函數(shù):將列表格式化打印出來 代碼部分 import requests from bs4 import BeautifulSoup import bs4 def getHtml(url, header): try: r = requests.get(url, headers=header) r.raise_for_status() print(r.request.headers) # r.encoding = r.apparent_encoding # 根據(jù)情況是否填寫 return r.text except: print("爬取失敗!") return " " def parsePage(ulist, html): soup = BeautifulSoup(html, "html.parser") for i in soup.find('main', {'class': 'site-main'}).children: try: if isinstance(i, bs4.element.Tag): psrc = i('div', {'class': 'p-time'}) title = i('h1', {'class': 'entry-title'}) # print(psrc[0].text) # print(title[0].string) # print(title[0].a.attrs['href']) ulist.append([psrc[0].text, title[0].string, title[0].a.attrs['href']]) # ulist.append([1, 1, 1]) except: print("數(shù)據(jù)丟失!") def printlist(ulist): print("{:10}\t{:10}\t{:8}".format("發(fā)布日期", "標(biāo)題", "鏈接")) for i in ulist: print("{:10}\t{:10}\t{:8}".format(i[0], i[1], i[2])) def main(): header = { "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36", } worlds = '1' ulist = [] url = "https://www.hellohuanxuan/?s=" + worlds html = getHtml(url, header) parsePage(ulist, html) printlist(ulist) if __name__ == "__main__": main() 代碼解析 getHtml try: # 通過requests的get方法獲得源代碼 r = requests.get(url, headers=header) # 判斷返回狀態(tài)碼是否為200,不為200直接進(jìn)入異常 r.raise_for_status() # 打印頭部信息看看,可注釋掉 print(r.request.headers) # r.encoding = r.apparent_encoding # 根據(jù)情況是否填寫,爬我的網(wǎng)站要注釋,否則顯示中文為亂碼 return r.text except: print("爬取失敗!") return " " parsePage # 利用BeautifulSoup解析html soup = BeautifulSoup(html, "html.parser") # for循環(huán)查找class為'site-main'的main標(biāo)簽的字標(biāo)簽 for i in soup.find('main', {'class': 'site-main'}).children: # try except捕捉異常 try: # isinstance函數(shù)在這里判斷i是否是bs4庫里規(guī)定的標(biāo)簽類型 if isinstance(i, bs4.element.Tag): # 獲取class為'p-time'的div標(biāo)簽 psrc = i('div', {'class': 'p-time'}) # 獲取class為'entry-title'的h1標(biāo)簽 title = i('h1', {'class': 'entry-title'}) # print(psrc[0].text) # print(title[0].string) # print(title[0].a.attrs['href']) # 將值寫進(jìn)列表 ulist.append([psrc[0].text, title[0].string, title[0].a.attrs['href']]) # ulist.append([1, 1, 1]) except: print("數(shù)據(jù)丟失!") printlist # 格式化輸出列表 print("{:10}\t{:10}\t{:8}".format("發(fā)布日期", "標(biāo)題", "鏈接")) for i in ulist: print("{:10}\t{:10}\t{:8}".format(i[0], i[1], i[2])) 運(yùn)行效果

          在這里插入圖片描述

          總結(jié)

          大家千萬別全拿我的網(wǎng)站爬啊,學(xué)生服務(wù)器經(jīng)不起太多折騰。(無奈) 最后推薦一個(gè)慕課的視頻,北京理工大學(xué)嵩天老師的python爬蟲課程,講的很清晰也很透徹。 Bilibili鏈接:python網(wǎng)絡(luò)爬蟲與信息提取 python爬蟲學(xué)習(xí)中,如果大佬們看出有什么可以優(yōu)化的地方歡迎指正 轉(zhuǎn)自自己的小網(wǎng)站:我的博客



          【本文地址】

          公司簡介

          聯(lián)系我們

          今日新聞

          推薦新聞

          專題文章
            CopyRight 2018-2019 實(shí)驗(yàn)室設(shè)備網(wǎng) 版權(quán)所有
            黄色免费网站在线看,韩国精品在线观看,韩国美女一区二区,99国产热 临潭县| 济南市| 郯城县| 新民市| 淮滨县| 布拖县| 治多县| 晋城| 厦门市| 大庆市| 肇源县| 伊通| 乐业县| 将乐县| 丹寨县| 唐河县| 连江县| 琼结县| 福贡县| 准格尔旗| 固原市| 万安县| 宾川县| 克山县| 桐柏县| 江永县| 蒙自县| 隆林| 齐齐哈尔市| 宿迁市| 娄底市| 吉安县| 滨海县| 上高县| 绥滨县| 桃园市| 青河县| 元江| 吉木萨尔县| 岳普湖县| 双流县| http://444 http://444 http://444 http://444 http://444 http://444