溫馨提示×

溫馨提示×

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

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

Python中如何爬取城市公交網(wǎng)絡站點數(shù)據(jù)

發(fā)布時間:2021-07-26 11:06:32 來源:億速云 閱讀:176 作者:小新 欄目:開發(fā)技術

這篇文章將為大家詳細講解有關Python中如何爬取城市公交網(wǎng)絡站點數(shù)據(jù),小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

(1)環(huán)境配置,直接上代碼:

# -*- coding: utf-8 -*-
import requests ##導入requests
from bs4 import BeautifulSoup ##導入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' ##開始的URL地址
start_html = requests.get(all_url, headers=headers) 
#print (start_html.text)
Soup = BeautifulSoup(start_html.text, 'lxml') # 以lxml的方式解析html文檔

(2)爬取站點分析

1、北京市公交線路分類方式有3種:

Python中如何爬取城市公交網(wǎng)絡站點數(shù)據(jù)

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

Python中如何爬取城市公交網(wǎng)絡站點數(shù)據(jù)

代碼

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)址,其內容即為線路名稱,代碼

href = a['href'] #取出a標簽的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不知道為啥取不出來,只好迂回取了

3、打開線路鏈接,就可以看到具體的站點信息了,打開頁面分析文檔結構后發(fā)現(xiàn):線路的基本信息存放在<div class="bus_i_content">里面,而公交站點信息則存放在<div class="bus_line_top"><div class="bus_line_site">里面,提取代碼:

Python中如何爬取城市公交網(wǎng)絡站點數(shù)據(jù)

title1 = a2.get_text() #取出a1標簽的文本
href1 = a2['href'] #取出a標簽的href 屬性
#print (title1,href1)
html_bus = all_url + href1 # 構建線路站點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('h2').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)線,也返回兩個list,只是其中一個為空
 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]

自此,我們就把一條線路的相關信息及上、下行站點信息就都解析出來了。如果想要爬取全市的公交網(wǎng)絡站點,只需要加入循環(huán)就可以了。

完整代碼:

# -*- coding: utf-8 -*-
# Python3.5
import requests ##導入requests
from bs4 import BeautifulSoup ##導入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' ##開始的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標簽的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不知道為啥取不出來,只好迂回取了
 for a2 in all_a2:
 title1 = a2.get_text() #取出a1標簽的文本
 href1 = a2['href'] #取出a標簽的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('h2').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)線,也返回兩個list,只是其中一個為空
  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ù),將運算結果保存為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');

最后輸出整個城市的公交網(wǎng)絡站點信息,這次就先保存在txt文件里吧,也可以保存到數(shù)據(jù)庫里,比如mysql或者MongoDB里,這里我就不寫了,有興趣的可以試一下,附上程序運行后的結果圖:

Python中如何爬取城市公交網(wǎng)絡站點數(shù)據(jù)

關于“Python中如何爬取城市公交網(wǎng)絡站點數(shù)據(jù)”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節(jié)

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

AI