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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

爬虫练习(2)-- 使用正则匹配爬取果壳问答

發(fā)布時(shí)間:2024/3/12 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 爬虫练习(2)-- 使用正则匹配爬取果壳问答 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

分析

從 web 的角度來(lái)看,網(wǎng)站架構(gòu)分為前后端分離和前后端不分離,如果是前后端不分離的結(jié)構(gòu),我們就需要從響應(yīng)中去匹配我們希望提取的數(shù)據(jù)。舉個(gè)例子就是 果殼網(wǎng)的熱門(mén)問(wèn)答。

獲取整個(gè)網(wǎng)頁(yè)

去正則匹配之前比較習(xí)慣先把代碼跑通,我們可以嘗試去構(gòu)建出這樣的結(jié)構(gòu),把當(dāng)前的整個(gè)網(wǎng)頁(yè)先打印出來(lái),再去調(diào)試

# coding:utf-8 import requests# <h2><a target="_blank" href="https://www.guokr.com/question/667020/">“三季人”是古文嗎?</a></h2> class Guoke(object):def __init__(self):self.headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}self.url = 'https://www.guokr.com/ask/hottest/'self.file = open('guoke.json', 'w')passdef get_data(self, url):response = requests.get(url, headers=self.headers)return response.contentdef parse_data(self, data):str_data = data.decode()print(str_data)def run(self):data = self.get_data(self.url)self.parse_data(data)def __del__(self):self.file.close()if __name__ == "__main__":guoke = Guoke()guoke.run()

運(yùn)行之后,我們可以發(fā)現(xiàn)我們已經(jīng)獲取了當(dāng)前的完整的網(wǎng)頁(yè)數(shù)據(jù)。

正則匹配果殼問(wèn)題

我們想要匹配的的是 <h2><a target="_blank" href="https://www.guokr.com/question/667020/">“三季人”是古文嗎?</a></h2>

正則的寫(xiě)法:<h2><a target="_blank" href="(.+?)">(.+?)</a></h2>
分別匹配鏈接和標(biāo)題 其中? 表示非貪婪模式

修改 parse_data 方法,

def parse_data(self, data):str_data = data.decode()res_list = re.findall('<h2><a target="_blank" href="(.+?)">(.+?)</a></h2>', str_data)print(res_list)

這次我們已經(jīng)提取到當(dāng)前頁(yè)的所有問(wèn)答的 url 和標(biāo)題

[('https://www.guokr.com/question/667020/', '“三季人”是古文嗎?'), ('https://www.guokr.com/question/392570/', 'EVA Q的這張海報(bào)是銀河的哪一段啊?'), ('https://www.guokr.com/question/677147/', '什么決定時(shí)間的流逝?物體內(nèi)動(dòng)能還是?'), ('https://www.guokr.com/question/582248/', '動(dòng)漫制作有那些過(guò)程?'), ('https://www.guokr.com/question/537650/', '干蝦為何有氨氣味?'), ('https://www.guokr.com/question/676492/', '寶寶總是喜歡吃辣條怎么辦?'), ('https://www.guokr.com/question/599265/', '精神病患者,怎樣說(shuō)服她去看醫(yī)生?'), ('https://www.guokr.com/question/559078/', '用普通塑料袋裝熱的食物,比如粥什么的,到底有沒(méi)有害?'), ('https://www.guokr.com/question/160920/', 'wifi信號(hào)會(huì)受什么的影響?'), ('https://www.guokr.com/question/492513/', '“死”字里為什么有個(gè)七夕?'), ('https://www.guokr.com/question/534950/', '科學(xué)青年如何寫(xiě)春聯(lián)?'), ('https://www.guokr.com/question/516692/', '你認(rèn)為最丑的公式有哪些?'), ('https://www.guokr.com/question/126413/', '有異地戀成功的嗎?你們是怎么熬過(guò)來(lái)的?'), ('https://www.guokr.com/question/506176/', '你是如何理解創(chuàng)業(yè)的?'), ('https://www.guokr.com/question/542184/', '《西游記》里為什么把唐僧描寫(xiě)的那么無(wú)能?'), ('https://www.guokr.com/question/203569/', '二戰(zhàn)美軍的降落傘質(zhì)檢的傳說(shuō)是真的嗎?'), ('https://www.guokr.com/question/676930/', '未來(lái)的果殼還有可能東山再起嗎?'), ('https://www.guokr.com/question/628160/', '青蛙能認(rèn)識(shí)自己生的寶寶嘛?'), ('https://www.guokr.com/question/545625/', '咖啡真的能降低自殺率嗎?'), ('https://www.guokr.com/question/669761/', '印度人把男人的生殖器叫林伽,把女人的生殖器叫瑜尼,林伽和瑜尼的交合,便是瑜伽。這是真還是假的')]

保存為 json 格式的數(shù)據(jù)

修改parse_data方法,將元組中的數(shù)據(jù)組合為字典,再添加到一個(gè)字典列表里面:

def parse_data(self, data):str_data = data.decode()print(str_data)res_list = re.findall('<h2><a target="_blank" href="(.+?)">(.+?)</a></h2>', str_data)temp_list = []for res in res_list:temp_dict = {}temp_dict["url"] = res[0]temp_dict["title"] = res[1]temp_list.append(temp_dict)print(temp_list)return temp_list

同時(shí)定義出數(shù)據(jù)的保存方法, save_data

def save_data(self, data_list):for data in data_list:str_data = json.dumps(data, ensure_ascii=False) + ',\n'self.file.write(str_data)

guoke.json 文件

{"title": "“三季人”是古文嗎?", "url": "https://www.guokr.com/question/667020/"}, {"title": "EVA Q的這張海報(bào)是銀河的哪一段啊?", "url": "https://www.guokr.com/question/392570/"}, {"title": "什么決定時(shí)間的流逝?物體內(nèi)動(dòng)能還是?", "url": "https://www.guokr.com/question/677147/"}, {"title": "動(dòng)漫制作有那些過(guò)程?", "url": "https://www.guokr.com/question/582248/"}, {"title": "干蝦為何有氨氣味?", "url": "https://www.guokr.com/question/537650/"}, {"title": "寶寶總是喜歡吃辣條怎么辦?", "url": "https://www.guokr.com/question/676492/"}, {"title": "精神病患者,怎樣說(shuō)服她去看醫(yī)生?", "url": "https://www.guokr.com/question/599265/"}, {"title": "用普通塑料袋裝熱的食物,比如粥什么的,到底有沒(méi)有害?", "url": "https://www.guokr.com/question/559078/"}, {"title": "wifi信號(hào)會(huì)受什么的影響?", "url": "https://www.guokr.com/question/160920/"}, {"title": "“死”字里為什么有個(gè)七夕?", "url": "https://www.guokr.com/question/492513/"}, {"title": "科學(xué)青年如何寫(xiě)春聯(lián)?", "url": "https://www.guokr.com/question/534950/"}, {"title": "你認(rèn)為最丑的公式有哪些?", "url": "https://www.guokr.com/question/516692/"}, {"title": "有異地戀成功的嗎?你們是怎么熬過(guò)來(lái)的?", "url": "https://www.guokr.com/question/126413/"}, {"title": "你是如何理解創(chuàng)業(yè)的?", "url": "https://www.guokr.com/question/506176/"}, {"title": "《西游記》里為什么把唐僧描寫(xiě)的那么無(wú)能?", "url": "https://www.guokr.com/question/542184/"}, {"title": "二戰(zhàn)美軍的降落傘質(zhì)檢的傳說(shuō)是真的嗎?", "url": "https://www.guokr.com/question/203569/"}, {"title": "未來(lái)的果殼還有可能東山再起嗎?", "url": "https://www.guokr.com/question/676930/"}, {"title": "青蛙能認(rèn)識(shí)自己生的寶寶嘛?", "url": "https://www.guokr.com/question/628160/"}, {"title": "咖啡真的能降低自殺率嗎?", "url": "https://www.guokr.com/question/545625/"}, {"title": "印度人把男人的生殖器叫林伽,把女人的生殖器叫瑜尼,林伽和瑜尼的交合,便是瑜伽。這是真還是假的", "url": "https://www.guokr.com/question/669761/"},

貌似 ip 被封了 … 一直504 …
過(guò)段時(shí)間接著寫(xiě) …

恢復(fù)了,換代理也不行,當(dāng)時(shí)應(yīng)該正好是是服務(wù)器掛了 …

實(shí)現(xiàn)翻頁(yè)

回憶聯(lián)系1里面,我們實(shí)現(xiàn)翻頁(yè)是去更改 ajax 請(qǐng)求的參數(shù)。對(duì)于數(shù)據(jù)和 url 在源碼中的形式,思路是找到“下一頁(yè)”對(duì)應(yīng)的 url 然后再去請(qǐng)求這個(gè) url ,循環(huán)上述的過(guò)程 …
代碼如下 :

# coding:utf-8 import requests import re import jsonclass Guoke(object):def __init__(self):self.headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}self.proxies = {'http':"http://183.163.40.223:31773",}self.url = 'https://www.guokr.com/ask/hottest/'self.file = open('guoke.json', 'w')def get_data(self, url):response = requests.get(url, proxies=self.proxies, headers=self.headers)print(response.status_code)# print(response.content)return response.contentdef parse_data(self, data):str_data = data.decode()print(str_data)res_list = re.findall('<h2><a target="_blank" href="(.+?)">(.+?)</a></h2>', str_data)temp_list = []for res in res_list:temp_dict = {}temp_dict["url"] = res[0]temp_dict["title"] = res[1]temp_list.append(temp_dict)# 匹配 "下一頁(yè)" 對(duì)應(yīng)的 url# <a href="/ask/hottest/?page=2">下一頁(yè)</a>next_url = re.findall('<a href="(/ask/hottest/\?page=\d+)">下一頁(yè)</a>', str_data)# print(next_url)# print(temp_list)if next_url:next_url = 'https://www.guokr.com' + next_url[0]return temp_list, next_urldef save_data(self, data_list):for data in data_list:str_data = json.dumps(data, ensure_ascii=False) + ',\n'self.file.write(str_data)def run(self):while True:data = self.get_data(self.url)data_list, next_url = self.parse_data(data)self.save_data(data_list)self.url = next_urlif not next_url:breakdef __del__(self):self.file.close()if __name__ == "__main__":guoke = Guoke()guoke.run()

查看json文件一共爬取了 64條數(shù)據(jù) … 跟網(wǎng)頁(yè)上的 4 頁(yè) 64條數(shù)據(jù)一致 ….

聯(lián)系方式

qq:2564493603
歡迎交流

總結(jié)

以上是生活随笔為你收集整理的爬虫练习(2)-- 使用正则匹配爬取果壳问答的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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