溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python爬取某婚戀網(wǎng)征婚數(shù)據(jù)的示例分析

發(fā)布時間:2021-06-04 09:31:52 來源:億速云 閱讀:279 作者:小新 欄目:開發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)Python爬取某婚戀網(wǎng)征婚數(shù)據(jù)的示例分析的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

一、打開界面

Python爬取某婚戀網(wǎng)征婚數(shù)據(jù)的示例分析

鼠標右鍵打開檢查,方框里為你一個文小姐的征婚信息。。由此判斷出為同步加載

Python爬取某婚戀網(wǎng)征婚數(shù)據(jù)的示例分析

點擊elements,定位圖片地址,方框里為該女士的url地址及圖片地址

Python爬取某婚戀網(wǎng)征婚數(shù)據(jù)的示例分析

可以看出該女士的url地址不全,之后在代碼中要進行url的拼接,看一下翻頁的url地址有什么變化

點擊第2頁
https://www.csflhjw.com/zhenghun/34.html?page=2

點擊第3頁
https://www.csflhjw.com/zhenghun/34.html?page=3

可以看出變化在最后
做一下fou循環(huán)格式化輸出一下。。一共10頁

Python爬取某婚戀網(wǎng)征婚數(shù)據(jù)的示例分析

二、代碼解析

1.獲取所有的女士的url,xpath的路徑就不詳細說了。。

Python爬取某婚戀網(wǎng)征婚數(shù)據(jù)的示例分析

2.構(gòu)造每一位女士的url地址

Python爬取某婚戀網(wǎng)征婚數(shù)據(jù)的示例分析

3.然后點開一位女士的url地址,用同樣的方法,確定也為同步加載

Python爬取某婚戀網(wǎng)征婚數(shù)據(jù)的示例分析

4.之后就是女士url地址html的xpath提取,每個都打印一下,把不要的過濾一下

Python爬取某婚戀網(wǎng)征婚數(shù)據(jù)的示例分析
Python爬取某婚戀網(wǎng)征婚數(shù)據(jù)的示例分析

5.最后就是文件的保存

Python爬取某婚戀網(wǎng)征婚數(shù)據(jù)的示例分析

打印結(jié)果:

Python爬取某婚戀網(wǎng)征婚數(shù)據(jù)的示例分析
Python爬取某婚戀網(wǎng)征婚數(shù)據(jù)的示例分析

三、完整代碼

# !/usr/bin/nev python
# -*-coding:utf8-*-

import requests, os, csv
from pprint import pprint
from lxml import etree

def main():
    for i in range(1, 11):
        start_url = 'https://www.csflhjw.com/zhenghun/34.html?page={}'.format(i)

        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
                          'Chrome/87.0.4280.88 Safari/537.36'

        }
        response = requests.get(start_url, headers=headers).content.decode()
        # # pprint(response)
        # 3 解析數(shù)據(jù)
        html_str = etree.HTML(response)
        info_urls = html_str.xpath(r'//div[@class="e"]/div[@class="e-img"]/a/@href')
        # pprint(info_urls)

        # 4、循環(huán)遍歷 構(gòu)造img_info_url
        for info_url in info_urls:
            info_url = r'https://www.csflhjw.com' + info_url
            # print(info_url)
            # 5、對info_url發(fā)請求,解析得到img_urls
            response = requests.get(info_url, headers=headers).content.decode()
            html_str = etree.HTML(response)
            # pprint(html_str)
            img_url = 'https://www.csflhjw.com/' + html_str.xpath(r'/html/body/div[4]/div/div[1]/div[2]/div[1]/div['
                                                                r'1]/img/@src')[0]
            # pprint(img_url)
            name = html_str.xpath(r'//div[@class="team-info"]/div[@class="team-e"]/h3/text()')[0]
            # pprint(name)
            xueli = html_str.xpath(r'//div[@class="team-info"]/div[@class="team-e"]/p[1]/text()')[0].split(':')[1]
            # pprint(xueli)
            job = html_str.xpath(r'//div[@class="team-info"]/div[@class="team-e"]/p[2]/text()')[0].split(':')[1]
            # pprint(job)
            marital_status = html_str.xpath(r'//div[@class="team-info"]/div[@class="team-e"]/p[3]/text()')[0].split(
                ':')[1]
            # pprint(marital_status)
            is_child = html_str.xpath(r'//div[@class="team-info"]/div[@class="team-e"]/p[4]/text()')[0].split(':')[1]
            # pprint(is_child)
            home = html_str.xpath(r'//div[@class="team-info"]/div[@class="team-e"]/p[5]/text()')[0].split(':')[1]
            # pprint(home)
            workplace = html_str.xpath(r'//div[@class="team-info"]/div[@class="team-e"]/p[6]/text()')[0].split(':')[1]
            # pprint(workplace)
            requ = html_str.xpath(r'/html/body/div[4]/div/div[1]/div[2]/div[2]/div[2]/p[2]/span/text()')[0].split(':')[1]
            # pprint(requ)
            requ = [requ if requ != str() else '無要求'][0]
            monologue = html_str.xpath(r'//div[@class="hunyin-1-3"]/p/text()')
            # pprint(monologue)
            monologue = [monologue[0].replace(' ', '').replace('\xa0', '') if monologue !=list() else '無'][0]
            # pprint(monologue)
            zeo_age = html_str.xpath(r'/html/body/div[4]/div/div[1]/div[2]/div[2]/div[2]/p[1]/span[1]/text()')[0].split(':')[1]
            zeo_age = [zeo_age if zeo_age!=str() else '無要求'][0]
            # pprint(zeo_age)
            zeo_address = html_str.xpath(r'/html/body/div[4]/div/div[1]/div[2]/div[2]/div[2]/p[1]/span[2]/text()')[0].split(':')[1]
            zeo_address = [zeo_address if zeo_address!=str() else '無要求'][0]
            # pprint(zeo_address)

            if not os.path.exists(r'./{}'.format('妹子信息數(shù)據(jù)')):
                os.mkdir(r'./{}'.format('妹子信息數(shù)據(jù)'))
                csv_header = ['姓名', '學(xué)歷', '職業(yè)', '婚姻狀況', '有無子女', '是否購房', '工作地點', '擇偶年齡', '擇偶城市', '擇偶要求', '個人獨白', '照片鏈接']
                with open(r'./{}/{}.csv'.format('妹子信息數(shù)據(jù)', '妹子數(shù)據(jù)'), 'w', newline='', encoding='gbk') as file_csv:
                    csv_writer_header = csv.DictWriter(file_csv, csv_header)
                    csv_writer_header.writeheader()

            try:
                with open(r'./{}/{}.csv'.format('妹子信息數(shù)據(jù)', '妹子數(shù)據(jù)'), 'a+', newline='',
                          encoding='gbk') as file_csv:
                    csv_writer = csv.writer(file_csv, delimiter=',')
                    csv_writer.writerow([name, xueli, job, marital_status, is_child, home, workplace, zeo_age,
                                         zeo_address, requ, monologue, img_url])
                    print(r'***妹子信息數(shù)據(jù):{}'.format(name))
            except Exception as e:
                with open(r'./{}/{}.csv'.format('妹子信息數(shù)據(jù)', '妹子數(shù)據(jù)'), 'a+', newline='',
                          encoding='utf-8') as file_csv:
                    csv_writer = csv.writer(file_csv, delimiter=',')
                    csv_writer.writerow([name, xueli, job, marital_status, is_child, home, workplace, zeo_age,
                                         zeo_address, requ, monologue, img_url])
                    print(r'***妹子信息數(shù)據(jù)保存成功:{}'.format(name))



if __name__ == '__main__':
    main()

感謝各位的閱讀!關(guān)于“Python爬取某婚戀網(wǎng)征婚數(shù)據(jù)的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI