python爬虫练习3:通过python爬取二手房源信息
生活随笔
收集整理的這篇文章主要介紹了
python爬虫练习3:通过python爬取二手房源信息
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
通過爬蟲爬取二手房源信息
- 前言
- 第一步:分析數據結構
- 第二步:寫代碼
- 1.引入庫
- 2.UA偽裝
- 第三步:我們用三種庫分別獲取數據
- 1:Xpath
- 2:Pyquery
- 3:BeautifulSoup
- 源碼
前言
目標網站:58同城二手房
爬蟲學了一段時間了,了解了request的用法,和其他一些網頁解析庫的用法,今天我整合一下幾個我了解過的庫
接下來我們開始進行寫代碼幾個步驟
第一步:分析數據結構
首先我們到目標網頁看看
F12,Element結構下,我們需要的數據是在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 UserAgent2.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 }第三步:我們用三種庫分別獲取數據
1:Xpath
代碼如下:
def Xpath():# 請求獲取網頁的數據respon = requests.get(url=url, headers=headers).text# 實例化對象tree = etree.HTML(respon)# xpath 的語法 '//' 在當前元素下獲取的匹配的所有內容(第一個//是根目錄下的所有內容)'/'是獲取子元素的內容,'[]'是獲取該標簽下的屬性值# 更詳細的語法可以去 https://www.w3school.com.cn/xpath/index.asp 這里看下# 下面的分別是獲取二手房名字所對應的標簽和價格所的對應的標簽name = tree.xpath('//ul[@class="house-list-wrap"]//h2[@class="title"]/a')price = tree.xpath('//p[@class="sum"]/b')# 在for循環外面打開文件,可以防止在for循環里面一遍又一遍的打開關閉文件而導致的性能消耗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這里可以直接獲取到網頁的內容# 相當于 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 具體用法可以看我前兩篇的內容,這里就不過多贅述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()感興趣的小伙伴可以看看我之前兩篇的內容
python爬蟲練習1:通過python爬取糗事百科的搞笑圖片
python爬蟲練習2:通過Python爬取小說
總結
以上是生活随笔為你收集整理的python爬虫练习3:通过python爬取二手房源信息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: webupload大文件上传的坑
- 下一篇: 独孤思维:小白搬运项目,房产公众号月入1