今天就跟大家聊聊有關(guān)使用python 爬蟲(chóng)爬取信息時(shí)會(huì)遇到哪些問(wèn)題,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
我們擁有十載網(wǎng)頁(yè)設(shè)計(jì)和網(wǎng)站建設(shè)經(jīng)驗(yàn),從網(wǎng)站策劃到網(wǎng)站制作,我們的網(wǎng)頁(yè)設(shè)計(jì)師為您提供的解決方案。為企業(yè)提供成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、微信開(kāi)發(fā)、微信小程序開(kāi)發(fā)、手機(jī)網(wǎng)站制作設(shè)計(jì)、H5建站、等業(yè)務(wù)。無(wú)論您有什么樣的網(wǎng)站設(shè)計(jì)或者設(shè)計(jì)方案要求,我們都將富于創(chuàng)造性的提供專業(yè)設(shè)計(jì)服務(wù)并滿足您的需求。爬取代碼:
import requests from requests.exceptions import RequestException from pyquery import PyQuery as pq from bs4 import BeautifulSoup import pymongo from config import * from multiprocessing import Pool client = pymongo.MongoClient(MONGO_URL) # 申明連接對(duì)象 db = client[MONGO_DB] # 申明數(shù)據(jù)庫(kù) def get_one_page_html(url): # 獲取網(wǎng)站每一頁(yè)的html headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) " "Chrome/85.0.4183.121 Safari/537.36" } try: response = requests.get(url, headers=headers) if response.status_code == 200: return response.text else: return None except RequestException: return None def get_room_url(html): # 獲取當(dāng)前頁(yè)面上所有room_info的url doc = pq(html) room_urls = doc('.r_lbx .r_lbx_cen .r_lbx_cena a').items() return room_urls def parser_room_page(room_html): soup = BeautifulSoup(room_html, 'lxml') title = soup.h2.text price = soup.find('div', {'class': 'room-price-sale'}).text[:-3] x = soup.find_all('div', {'class': 'room-list'}) area = x[0].text[7:-11] # 面積 bianhao = x[1].text[4:] house_type = x[2].text.strip()[3:7] # 戶型 floor = x[5].text[4:-2] # 樓層 location1 = x[6].find_all('a')[0].text # 分區(qū) location2 = x[6].find_all('a')[1].text location3 = x[6].find_all('a')[2].text subway = x[7].text[4:] addition = soup.find_all('div', {'class': 'room-title'})[0].text yield { 'title': title, 'price': price, 'area': area, 'bianhao': bianhao, 'house_type': house_type, 'floor': floor, 'location1': location1, 'location2': location2, 'location3': location3, 'subway': subway, 'addition': addition } def save_to_mongo(result): if db[MONGO_TABLE].insert_one(result): print('存儲(chǔ)到mongodb成功', result) return True return False def main(page): url = 'http://www.xxxxx.com/room/sz?page=' + str(page) # url就不粘啦,嘻嘻 html = get_one_page_html(url) room_urls = get_room_url(html) for room_url in room_urls: room_url_href = room_url.attr('href') room_html = get_one_page_html(room_url_href) if room_html is None: # 非常重要,否則room_html為None時(shí)會(huì)報(bào)錯(cuò) pass else: results = parser_room_page(room_html) for result in results: save_to_mongo(result) if __name__ == '__main__': pool = Pool() # 使用多進(jìn)程提高爬取效率 pool.map(main, [i for i in range(1, 258)])
當(dāng)前名稱:使用python爬蟲(chóng)爬取信息時(shí)會(huì)遇到哪些問(wèn)題-創(chuàng)新互聯(lián)
文章起源:http://jinyejixie.com/article12/dpdcgc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、域名注冊(cè)、面包屑導(dǎo)航、微信公眾號(hào)、做網(wǎng)站、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容