python 爬虫-养生之道
生活随笔
收集整理的這篇文章主要介紹了
python 爬虫-养生之道
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Bs 最近開始養生之道,需要一些材料。
所謂養生,動詞也,亦可為名詞。原指道家通過各種方法頤養生命、增強體質、預防疾病,從而達到延年益壽的一種醫事活動。養,即調養、保養、補養之意;生,即生命、生存、生長之意。現代意義的“養生”指的是根據人的生命過程規律主動進行物質與精神的身心養護活動。
1.目標
- http://jlxw.jiudaifu.com/
- 定位元素,有一個 href。
- 詳情頁的 url = 主頁的url + href。
2.開始獲取所有的詳情頁 href
- """作者:mldsh日期:2022年08月26日10:20使用工具:PyCharm """ import requests from icecream import ic from lxml import etreeclass AcuPoint(object):def __init__(self):# 主頁 urlself.url = 'http://jlxw.jiudaifu.com/'# 獲取主頁 htmldef get_index_html(self):# 發送 requests 請求res = requests.get(url=self.url).textreturn res# 獲取主頁所有的 hrefdef get_href(self):index_html = self.get_index_html()tree = etree.HTML(index_html)detail_href = tree.xpath('//li[@class="menu_s center"]')for href_path in detail_href:href = href_path.xpath('./div/a/@href')ic(href)if __name__ == '__main__':acu = AcuPoint()acu.get_href()
-
看到第一個 href 是 “xinfo/70”
- 跑出來的第一個結果也是一樣。
- 在看最后一個,一樣的,說明主頁href 全部獲取完成。
3.拼接詳情頁的 url,定位元素,獲取數據。
- """作者:mldsh日期:2022年08月26日10:20使用工具:PyCharm """ import requests from icecream import ic from lxml import etreeclass AcuPoint(object):def __init__(self):# 主頁 urlself.url = 'http://jlxw.jiudaifu.com/'# 獲取主頁 htmldef get_index_html(self):# 發送 requests 請求res = requests.get(url=self.url).textreturn res# 獲取主頁所有的 hrefdef get_href(self):all_url = {}index_html = self.get_index_html()tree = etree.HTML(index_html)li = tree.xpath('//li[@class="menu center"]')for li_href in li:theme_url = li_href.xpath('./a/@href')theme_name = li_href.xpath('./a/span/text()')href = li_href.xpath('./ul/li[@class="menu_s center"]/div/a/@href')href_name = li_href.xpath('./ul/li[@class="menu_s center"]/div/a/span/text()')all_url[theme_name[0]] = {self.url + theme_url[0]: dict(zip(href_name, [self.url + i for i in href]))}return all_url# 詳情頁數據def detail_data(self):detail_url_dict = self.get_href()for detail_name in detail_url_dict:# print(list(detail_url_dict[detail_name].keys())[0],detail_name)res_html = requests.get(url=list(detail_url_dict[detail_name].keys())[0]).texttree = etree.HTML(res_html)div = tree.xpath('//div[@class="main"]')for h2_data in div:title = h2_data.xpath('./ul/li/h2/text()')text = h2_data.xpath('./ul/li/div/text()')img_url = h2_data.xpath('./ul/li/div/img/@src')text.extend(img_url)ic(title, text)returnif __name__ == '__main__':acu = AcuPoint()acu.detail_data()
總結
以上是生活随笔為你收集整理的python 爬虫-养生之道的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 配置 VScode 编辑器 (前端篇)
- 下一篇: Android camera2扫描