網上那麼多的窮遊攻略該看那個?固然是全都要了,Python幫你爬取

前言php

本文的文字及圖片來源於網絡,僅供學習、交流使用,不具備任何商業用途,版權歸原做者全部,若有問題請及時聯繫咱們以做處理。html

做者:IT共享者面試

喜歡的朋友歡迎關注小編,除了分享技術文章以外還有不少福利chrome

加企鵝羣695185429便可免費獲取,資料全在羣文件裏。資料能夠領取包括不限於Python實戰演練、PDF電子文檔、面試集錦、學習資料等數組

項目背景

窮遊網提供原創實用的出境遊旅行指南、攻略,旅行社區和問答交流平臺,以及智能的旅行規劃解決方案,同時提供簽證、保險、機票、酒店預訂、租車等在線增值服務。窮遊「鼓勵和幫助中國旅行者以本身的視角和方式體驗世界」。瀏覽器

今天教你們獲取窮遊網的城市信息,使用Python將數據寫入csv文檔。網絡

網上那麼多的窮遊攻略該看那個?固然是全都要了,Python幫你爬取

 

項目目標

實現將獲取對應的城市、圖片連接、熱點、批量下載 保存csv文檔。app

涉及的庫和網站

一、網址以下:dom

https://place.qyer.com/south-korea/citylist-0-0-{}

 

二、涉及的庫:requests、lxml、fake_useragent、time、csvide

項目分析

首先須要解決如何對下一頁的網址進行請求的問題。能夠點擊下一頁的按鈕,觀察到網站的變化分別以下所示:

- https://place.qyer.com/south-korea/citylist-0-0-1

- https://place.qyer.com/south-korea/citylist-0-0-2

- https://place.qyer.com/south-korea/citylist-0-0-3

 

觀察到只有citylist-0-0-{}/變化,變化的部分用{}代替,再用for循環遍歷這網址,實現多個網址請求。

項目實施

一、咱們定義一個class類繼承object,而後定義init方法繼承self,再定義一個主函數main繼承self。準備url地址。

import requests,os
from lxml import etree
import random
import time
from fake_useragent import UserAgent


class Travel(object):
    def __init__(self):
        self.url = "https://place.qyer.com/south-korea/citylist-0-0-{}/"
    def main(self):
        pass
if __name__ == '__main__':
    spider= Travel()
    spider.main()

 

二、隨機產生UserAgent。

        self.film_list = []
        ua = UserAgent(verify_ssl=False)
        for i in range(1, 50):
            self.film_list.append(ua.chrome)
            self.Hostreferer = {
            'User-Agent': random.choice(self.film_list)
        }

 

 

三、多頁請求。

        startPage = int(input("起始頁:"))
        endPage = int(input("終止頁:"))
        for page in range(startPage, endPage + 1):
            url = self.url.format(page)

 

四、定義get_page方法,進行數據請求。

   '''數據請求'''
    def get_page(self, url):
        html = requests.get(url=url, headers=self.Hostreferer).content.decode("utf-8")
        self.page_page(html)
        # print(html)//網頁
        # print(self.headers)//構造頭

 

五、定義page_page,xpath解析數據, for循環遍歷數組。

在谷歌瀏覽器上,右鍵選擇開發者工具或者按F12。右鍵檢查,xpath找到對應信息的字段。以下圖所示。

網上那麼多的窮遊攻略該看那個?固然是全都要了,Python幫你爬取

 

    parse_html = etree.HTML(html)
    image_src_list = parse_html.xpath('//ul[@class="plcCitylist"]/li') 
    for i in image_src_list:
            b = i.xpath('.//h3//a/text()')[0].strip()
            c = i.xpath('.//p[@class="beento"]//text()')[0].strip()
            d = i.xpath('.//p[@class="pics"]//img//@src')[0].strip()

 

六、定義csv文件保存數據,寫入文檔。

        # 建立csv文件進行寫入
        csv_file = open('scrape.csv', 'a', encoding='gbk')
        csv_writer = csv.writer(csv_file)
        csv_writer.writerow([b, c, d])
        csv_file.close(

 

七、在main方法調用。

    def main(self):
        for i1 in range(1, 25):
            url = self.url.format(i1)
            # print(url)
            html = self.get_page(url)

            time.sleep(2)
            print("第%d頁" % i1)

 

八、time模塊,設置時間延遲。

         time.sleep(2)

 

效果展現

一、點擊運行,輸入起始頁,終止頁。

網上那麼多的窮遊攻略該看那個?固然是全都要了,Python幫你爬取

 

二、將下載成功信息顯示在控制檯。

網上那麼多的窮遊攻略該看那個?固然是全都要了,Python幫你爬取

 

三、保存csv文檔。

網上那麼多的窮遊攻略該看那個?固然是全都要了,Python幫你爬取
相關文章
相關標籤/搜索