Python爬蟲(chóng)實(shí)例_城市公交網(wǎng)絡(luò)站點(diǎn)數(shù)據(jù)的爬取方法
爬取的站點(diǎn):http://beijing.8684.cn/

(1)環(huán)境配置,直接上代碼:
# -*- coding: utf-8 -*-
import requests ##導(dǎo)入requests
from bs4 import BeautifulSoup ##導(dǎo)入bs4中的BeautifulSoup
import os
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0'}
all_url = 'http://beijing.8684.cn' ##開(kāi)始的URL地址
start_html = requests.get(all_url, headers=headers)
#print (start_html.text)
Soup = BeautifulSoup(start_html.text, 'lxml') # 以lxml的方式解析html文檔
(2)爬取站點(diǎn)分析
1、北京市公交線路分類方式有3種:

本文通過(guò)數(shù)字開(kāi)頭來(lái)進(jìn)行爬取,“F12”啟動(dòng)開(kāi)發(fā)者工具,點(diǎn)擊“Elements”,點(diǎn)擊“1”,可以發(fā)現(xiàn)鏈接保存在<div class="bus_kt_r1">里面,故只需要提取出div里的href即可:

代碼:
all_a = Soup.find(‘div',class_='bus_kt_r1').find_all(‘a(chǎn)')
2、接著往下,發(fā)現(xiàn)每1路的鏈接都在<div id="con_site_1" class="site_list"> 的<a>里面,取出里面的herf即為線路網(wǎng)址,其內(nèi)容即為線路名稱,代碼:
href = a['href'] #取出a標(biāo)簽的href 屬性
html = all_url + href
second_html = requests.get(html,headers=headers)
#print (second_html.text)
Soup2 = BeautifulSoup(second_html.text, 'lxml')
all_a2 = Soup2.find('div',class_='cc_content').find_all('div')[-1].find_all('a') # 既有id又有class的div不知道為啥取不出來(lái),只好迂回取了
3、打開(kāi)線路鏈接,就可以看到具體的站點(diǎn)信息了,打開(kāi)頁(yè)面分析文檔結(jié)構(gòu)后發(fā)現(xiàn):線路的基本信息存放在<div class="bus_i_content">里面,而公交站點(diǎn)信息則存放在<div class="bus_line_top">及<div class="bus_line_site">里面,提取代碼:

title1 = a2.get_text() #取出a1標(biāo)簽的文本
href1 = a2['href'] #取出a標(biāo)簽的href 屬性
#print (title1,href1)
html_bus = all_url + href1 # 構(gòu)建線路站點(diǎn)url
thrid_html = requests.get(html_bus,headers=headers)
Soup3 = BeautifulSoup(thrid_html.text, 'lxml')
bus_name = Soup3.find('div',class_='bus_i_t1').find('h1').get_text() # 提取線路名
bus_type = Soup3.find('div',class_='bus_i_t1').find('a').get_text() # 提取線路屬性
bus_time = Soup3.find_all('p',class_='bus_i_t4')[0].get_text() # 運(yùn)行時(shí)間
bus_cost = Soup3.find_all('p',class_='bus_i_t4')[1].get_text() # 票價(jià)
bus_company = Soup3.find_all('p',class_='bus_i_t4')[2].find('a').get_text() # 公交公司
bus_update = Soup3.find_all('p',class_='bus_i_t4')[3].get_text() # 更新時(shí)間
bus_label = Soup3.find('div',class_='bus_label')
if bus_label:
bus_length = bus_label.get_text() # 線路里程
else:
bus_length = []
#print (bus_name,bus_type,bus_time,bus_cost,bus_company,bus_update)
all_line = Soup3.find_all('div',class_='bus_line_top') # 線路簡(jiǎn)介
all_site = Soup3.find_all('div',class_='bus_line_site')# 公交站點(diǎn)
line_x = all_line[0].find('div',class_='bus_line_txt').get_text()[:-9]+all_line[0].find_all('span')[-1].get_text()
sites_x = all_site[0].find_all('a')
sites_x_list = [] # 上行線路站點(diǎn)
for site_x in sites_x:
sites_x_list.append(site_x.get_text())
line_num = len(all_line)
if line_num==2: # 如果存在環(huán)線,也返回兩個(gè)list,只是其中一個(gè)為空
line_y = all_line[1].find('div',class_='bus_line_txt').get_text()[:-9]+all_line[1].find_all('span')[-1].get_text()
sites_y = all_site[1].find_all('a')
sites_y_list = [] # 下行線路站點(diǎn)
for site_y in sites_y:
sites_y_list.append(site_y.get_text())
else:
line_y,sites_y_list=[],[]
information = [bus_name,bus_type,bus_time,bus_cost,bus_company,bus_update,bus_length,line_x,sites_x_list,line_y,sites_y_list]
自此,我們就把一條線路的相關(guān)信息及上、下行站點(diǎn)信息就都解析出來(lái)了。如果想要爬取全市的公交網(wǎng)絡(luò)站點(diǎn),只需要加入循環(huán)就可以了。
完整代碼:
# -*- coding: utf-8 -*-
# Python3.5
import requests ##導(dǎo)入requests
from bs4 import BeautifulSoup ##導(dǎo)入bs4中的BeautifulSoup
import os
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0'}
all_url = 'http://beijing.8684.cn' ##開(kāi)始的URL地址
start_html = requests.get(all_url, headers=headers)
#print (start_html.text)
Soup = BeautifulSoup(start_html.text, 'lxml')
all_a = Soup.find('div',class_='bus_kt_r1').find_all('a')
Network_list = []
for a in all_a:
href = a['href'] #取出a標(biāo)簽的href 屬性
html = all_url + href
second_html = requests.get(html,headers=headers)
#print (second_html.text)
Soup2 = BeautifulSoup(second_html.text, 'lxml')
all_a2 = Soup2.find('div',class_='cc_content').find_all('div')[-1].find_all('a') # 既有id又有class的div不知道為啥取不出來(lái),只好迂回取了
for a2 in all_a2:
title1 = a2.get_text() #取出a1標(biāo)簽的文本
href1 = a2['href'] #取出a標(biāo)簽的href 屬性
#print (title1,href1)
html_bus = all_url + href1
thrid_html = requests.get(html_bus,headers=headers)
Soup3 = BeautifulSoup(thrid_html.text, 'lxml')
bus_name = Soup3.find('div',class_='bus_i_t1').find('h1').get_text()
bus_type = Soup3.find('div',class_='bus_i_t1').find('a').get_text()
bus_time = Soup3.find_all('p',class_='bus_i_t4')[0].get_text()
bus_cost = Soup3.find_all('p',class_='bus_i_t4')[1].get_text()
bus_company = Soup3.find_all('p',class_='bus_i_t4')[2].find('a').get_text()
bus_update = Soup3.find_all('p',class_='bus_i_t4')[3].get_text()
bus_label = Soup3.find('div',class_='bus_label')
if bus_label:
bus_length = bus_label.get_text()
else:
bus_length = []
#print (bus_name,bus_type,bus_time,bus_cost,bus_company,bus_update)
all_line = Soup3.find_all('div',class_='bus_line_top')
all_site = Soup3.find_all('div',class_='bus_line_site')
line_x = all_line[0].find('div',class_='bus_line_txt').get_text()[:-9]+all_line[0].find_all('span')[-1].get_text()
sites_x = all_site[0].find_all('a')
sites_x_list = []
for site_x in sites_x:
sites_x_list.append(site_x.get_text())
line_num = len(all_line)
if line_num==2: # 如果存在環(huán)線,也返回兩個(gè)list,只是其中一個(gè)為空
line_y = all_line[1].find('div',class_='bus_line_txt').get_text()[:-9]+all_line[1].find_all('span')[-1].get_text()
sites_y = all_site[1].find_all('a')
sites_y_list = []
for site_y in sites_y:
sites_y_list.append(site_y.get_text())
else:
line_y,sites_y_list=[],[]
information = [bus_name,bus_type,bus_time,bus_cost,bus_company,bus_update,bus_length,line_x,sites_x_list,line_y,sites_y_list]
Network_list.append(information)
# 定義保存函數(shù),將運(yùn)算結(jié)果保存為txt文件
def text_save(content,filename,mode='a'):
# Try to save a list variable in txt file.
file = open(filename,mode)
for i in range(len(content)):
file.write(str(content[i])+'\n')
file.close()
# 輸出處理后的數(shù)據(jù)
text_save(Network_list,'Network_bus.txt');
最后輸出整個(gè)城市的公交網(wǎng)絡(luò)站點(diǎn)信息,這次就先保存在txt文件里吧,也可以保存到數(shù)據(jù)庫(kù)里,比如mysql或者M(jìn)ongoDB里,這里我就不寫了,有興趣的可以試一下,附上程序運(yùn)行后的結(jié)果圖:

以上這篇Python爬蟲(chóng)實(shí)例_城市公交網(wǎng)絡(luò)站點(diǎn)數(shù)據(jù)的爬取方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決Python報(bào)錯(cuò)No module named Crypto問(wèn)題
這篇文章主要介紹了解決Python報(bào)錯(cuò)No module named“Crypto”問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
Python用Bottle輕量級(jí)框架進(jìn)行Web開(kāi)發(fā)
這篇文章主要介紹了Python用Bottle輕量級(jí)框架進(jìn)行Web開(kāi)發(fā)的相關(guān)資料,需要的朋友可以參考下2016-06-06
對(duì)Python中l(wèi)ist的倒序索引和切片實(shí)例講解
今天小編就為大家分享一篇對(duì)Python中l(wèi)ist的倒序索引和切片實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
python的pygal模塊繪制反正切函數(shù)圖像方法
在本篇文章中我們給大家整理了關(guān)于如何用python的pygal模塊繪制反正切函數(shù)圖像的知識(shí)點(diǎn)內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2019-07-07

