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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python爬虫练习3:通过python爬取二手房源信息

發(fā)布時間:2023/12/29 python 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python爬虫练习3:通过python爬取二手房源信息 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

通過爬蟲爬取二手房源信息

  • 前言
  • 第一步:分析數(shù)據(jù)結(jié)構(gòu)
  • 第二步:寫代碼
    • 1.引入庫
    • 2.UA偽裝
  • 第三步:我們用三種庫分別獲取數(shù)據(jù)
    • 1:Xpath
    • 2:Pyquery
    • 3:BeautifulSoup
  • 源碼

前言

目標網(wǎng)站:58同城二手房

爬蟲學了一段時間了,了解了request的用法,和其他一些網(wǎng)頁解析庫的用法,今天我整合一下幾個我了解過的庫

接下來我們開始進行寫代碼幾個步驟

第一步:分析數(shù)據(jù)結(jié)構(gòu)

首先我們到目標網(wǎng)頁看看

F12,Element結(jié)構(gòu)下,我們需要的數(shù)據(jù)是在ul.house-list-wrap類里面

li.sendsoj類下面的div.list-info類里面的h2.title類里面的a標簽中


同理,我們需要的價格信息在這一塊

第二步:寫代碼

1.引入庫

import requests from lxml import etree from bs4 import BeautifulSoup from pyquery import PyQuery as pq from fake_useragent import UserAgent

2.UA偽裝

ua = UserAgent()url = 'https://hz.58.com/xihuqu/ershoufang/?utm_source=sem-sales-baidu-pc&spm=62851881867.16537920592&utm_campaign=sell&utm_medium=cpc&showpjs=pc_fg&PGTID=0d30000c-0004-f0a2-62bf-52886ad31056&ClickID=1' headers = {"User-Agent": ua.chrome }

第三步:我們用三種庫分別獲取數(shù)據(jù)

1:Xpath

代碼如下:

def Xpath():# 請求獲取網(wǎng)頁的數(shù)據(jù)respon = requests.get(url=url, headers=headers).text# 實例化對象tree = etree.HTML(respon)# xpath 的語法 '//' 在當前元素下獲取的匹配的所有內(nèi)容(第一個//是根目錄下的所有內(nèi)容)'/'是獲取子元素的內(nèi)容,'[]'是獲取該標簽下的屬性值# 更詳細的語法可以去 https://www.w3school.com.cn/xpath/index.asp 這里看下# 下面的分別是獲取二手房名字所對應(yīng)的標簽和價格所的對應(yīng)的標簽name = tree.xpath('//ul[@class="house-list-wrap"]//h2[@class="title"]/a')price = tree.xpath('//p[@class="sum"]/b')# 在for循環(huán)外面打開文件,可以防止在for循環(huán)里面一遍又一遍的打開關(guān)閉文件而導(dǎo)致的性能消耗f = open('58二手房源和價格.txt', 'a', encoding='utf-8')for index, i in enumerate(name):# 以為xpath獲取的是列表,所以我們在這里給他拿到里面的文字-->i.xpath('./text()')[0]# 這里分別是獲取單個的名字和價格,分別寫入文件homeName = i.xpath('./text()')[0]howMuch = price[index].xpath('./text()')[0]f.write('名稱:' + homeName + '\t')f.write('價格:' + howMuch + '\n')f.close()

2:Pyquery

代碼如下:

def Pyquery():# pyquery這里可以直接獲取到網(wǎng)頁的內(nèi)容# 相當于 html = pq(requests.get(url=url, headers=headers).text)html = pq(url=url)# 獲取到里面的元素# 在pyquery里面# '.'是類名,空格是獲取到他的后代元素,'>'是獲取到他的子代元素# 這里是獲取到他的名字的列表和價格的列表names = html('ul.house-list-wrap h2.title a')prices = html('p.sum b')# 打開文件f = open('58二手房源和價格.txt', 'a', encoding='utf-8')for index, name in enumerate(names):# 以為pyquery里面獲取的是他本身的對象,所以我們在這里給他拿到里面的文字 --> name.text# 這里分別是獲取單個的名字和價格,分別寫入文件f.write('名稱:' + name.text + '\t')f.write('價格:' + prices[index].text + '\n')f.close()

3:BeautifulSoup

代碼如下:

def BeautifulSoups():# BeautifulSoup 具體用法可以看我前兩篇的內(nèi)容,這里就不過多贅述respon = requests.get(url=url, headers=headers).textsoup = BeautifulSoup(respon, 'lxml')names = soup.select('ul.house-list-wrap h2.title a')prices = soup.select('p.sum b')f = open('58二手房源和價格.txt', 'a', encoding='utf-8')for index, name in enumerate(names):f.write('名稱:' + name.text + '\t')f.write('價格:' + prices[index].text + '\n')f.close()

接下來話不多說,直接看源碼

源碼

import requests from lxml import etree from bs4 import BeautifulSoup from pyquery import PyQuery as pq from fake_useragent import UserAgentua = UserAgent()url = 'https://hz.58.com/xihuqu/ershoufang/?utm_source=sem-sales-baidu-pc&spm=62851881867.16537920592&utm_campaign=sell&utm_medium=cpc&showpjs=pc_fg&PGTID=0d30000c-0004-f0a2-62bf-52886ad31056&ClickID=1' headers = {"User-Agent": ua.chrome }def Xpath():respon = requests.get(url=url, headers=headers).texttree = etree.HTML(respon)name = tree.xpath('//ul[@class="house-list-wrap"]//h2[@class="title"]/a')price = tree.xpath('//p[@class="sum"]/b')f = open('58二手房源和價格.txt', 'a', encoding='utf-8')for index, i in enumerate(name):homeName = i.xpath('./text()')[0]howMuch = price[index].xpath('./text()')[0]f.write('名稱:' + homeName + '\t')f.write('價格:' + howMuch + '\n')f.close()def Pyquery():html = pq(url=url)names = html('ul.house-list-wrap h2.title a')prices = html('p.sum b')f = open('58二手房源和價格.txt', 'a', encoding='utf-8')for index, name in enumerate(names):f.write('名稱:' + name.text + '\t')f.write('價格:' + prices[index].text + '\n')f.close()def BeautifulSoups():respon = requests.get(url=url, headers=headers).textsoup = BeautifulSoup(respon, 'lxml')names = soup.select('ul.house-list-wrap h2.title a')prices = soup.select('p.sum b')f = open('58二手房源和價格.txt', 'a', encoding='utf-8')for index, name in enumerate(names):f.write('名稱:' + name.text + '\t')f.write('價格:' + prices[index].text + '\n')f.close()def main():# Xpath()# Pyquery()BeautifulSoups()if __name__ == "__main__":main()

感興趣的小伙伴可以看看我之前兩篇的內(nèi)容
python爬蟲練習1:通過python爬取糗事百科的搞笑圖片
python爬蟲練習2:通過Python爬取小說

總結(jié)

以上是生活随笔為你收集整理的python爬虫练习3:通过python爬取二手房源信息的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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