【菜鸟学Python】爬取果壳问答
生活随笔
收集整理的這篇文章主要介紹了
【菜鸟学Python】爬取果壳问答
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
爬取流程
1.確定url
2.請求url
3.使用xpath處理數據
4.保存數據
import time import json import requests from lxml import etreeclass GuoKe(object):def __init__(self):self.base_url = 'https://www.guokr.com/ask/hottest/'self.headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.108 Safari/537.36","Referer": "https://www.guokr.com/"}def get_data(self, url, page, is_detail=False):if not is_detail:param = {"page": page}return requests.get(url, headers=self.headers, params=param).textreturn requests.get(url, headers=self.headers).textdef parse_text(self, text):dom = etree.HTML(text)return domdef detail_dom(self, dom):answer = dom.xpath('//div[@id="questionDesc"]/p/text()')[0]return answerdef parse(self, dom):nodes = dom.xpath('//div[@class="ask-list-detials"]')for node in nodes:item = {}item['title'] = node.xpath('./h2/a/text()')[0]detail_url = node.xpath('./h2/a/@href')[0]print(detail_url)detail_text = self.get_data(detail_url, True)detail_dom = etree.HTML(detail_text)answer = self.detail_dom(detail_dom)print(answer)item['answer'] = answerprint(item)yield itemdef save(self, f, item):f.write(json.dumps(item, ensure_ascii=False) + ',\n')def run(self):with open('guoke.json', 'w')as f:page = int(input('請輸入頁碼:'))for i in range(page):text = self.get_data(self.base_url, i + 1)dom = self.parse_text(text)my_generator = self.parse(dom)while True:try:item = next(my_generator)self.save(f, item)time.sleep(0.4)except:breakprint(f'第{i + 1}頁保存完成')if __name__ == '__main__':guoke = GuoKe()guoke.run()?
轉載于:https://www.cnblogs.com/liduo0413/p/11471088.html
總結
以上是生活随笔為你收集整理的【菜鸟学Python】爬取果壳问答的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【考研复试】计算机相关专业面试英语自我介
- 下一篇: Springboot 项目整合自定义日志