欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python爬蟲實例_城市公交網(wǎng)絡(luò)站點數(shù)據(jù)的爬取方法

 更新時間:2018年01月10日 09:45:26   作者:WenWu_Both  
下面小編就為大家分享一篇Python爬蟲實例_城市公交網(wǎng)絡(luò)站點數(shù)據(jù)的爬取方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

爬取的站點: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' ##開始的URL地址
start_html = requests.get(all_url, headers=headers) 
#print (start_html.text)
Soup = BeautifulSoup(start_html.text, 'lxml') # 以lxml的方式解析html文檔

(2)爬取站點分析

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

本文通過數(shù)字開頭來進行爬取,“F12”啟動開發(fā)者工具,點擊“Elements”,點擊“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不知道為啥取不出來,只好迂回取了

3、打開線路鏈接,就可以看到具體的站點信息了,打開頁面分析文檔結(jié)構(gòu)后發(fā)現(xiàn):線路的基本信息存放在<div class="bus_i_content">里面,而公交站點信息則存放在<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)建線路站點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() # 運行時間
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]

自此,我們就把一條線路的相關(guān)信息及上、下行站點信息就都解析出來了。如果想要爬取全市的公交網(wǎng)絡(luò)站點,只需要加入循環(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' ##開始的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不知道為啥取不出來,只好迂回取了
 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)線,也返回兩個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ù),將運算結(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'); 

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

以上這篇Python爬蟲實例_城市公交網(wǎng)絡(luò)站點數(shù)據(jù)的爬取方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 教你用Python下載抖音無水印視頻

    教你用Python下載抖音無水印視頻

    這篇文章主要介紹了教你用Python下載抖音無水印視頻,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)python的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-05-05
  • python與pycharm有何區(qū)別

    python與pycharm有何區(qū)別

    在本篇文章里小編給大家整理了關(guān)于pycharm與python的區(qū)別相關(guān)內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。
    2020-07-07
  • python中文件操作與異常的處理圖文詳解

    python中文件操作與異常的處理圖文詳解

    在實際開發(fā)中常常需要對程序中的數(shù)據(jù)進行持久化操作,而實現(xiàn)數(shù)據(jù)持久化最直接簡單的方式就是將數(shù)據(jù)保存到文件中,下面這篇文章主要給大家介紹了關(guān)于python中文件操作與異常處理的相關(guān)資料,需要的朋友可以參考下
    2021-12-12
  • Python批量修改文件名案例匯總

    Python批量修改文件名案例匯總

    在文件管理和數(shù)據(jù)處理中,批量修改文件名是一項常見且重要的任務(wù),Python作為一種功能強大的編程語言,提供了豐富的庫和工具來簡化這一過程,本文將結(jié)合實際案例,詳細(xì)介紹如何通過Python批量修改文件名,需要的朋友可以參考下
    2024-08-08
  • 解決Python報錯No module named Crypto問題

    解決Python報錯No module named Crypto問題

    這篇文章主要介紹了解決Python報錯No module named“Crypto”問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Python變量的作用域使用

    Python變量的作用域使用

    這篇文章主要介紹了Python變量的作用域使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Python用Bottle輕量級框架進行Web開發(fā)

    Python用Bottle輕量級框架進行Web開發(fā)

    這篇文章主要介紹了Python用Bottle輕量級框架進行Web開發(fā)的相關(guān)資料,需要的朋友可以參考下
    2016-06-06
  • django中path和url函數(shù)的具體使用

    django中path和url函數(shù)的具體使用

    本文主要介紹了django中path和url函數(shù)的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • 對Python中l(wèi)ist的倒序索引和切片實例講解

    對Python中l(wèi)ist的倒序索引和切片實例講解

    今天小編就為大家分享一篇對Python中l(wèi)ist的倒序索引和切片實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • python的pygal模塊繪制反正切函數(shù)圖像方法

    python的pygal模塊繪制反正切函數(shù)圖像方法

    在本篇文章中我們給大家整理了關(guān)于如何用python的pygal模塊繪制反正切函數(shù)圖像的知識點內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。
    2019-07-07

最新評論