使用Python的Requests/BeautifuiSoup模块进行城市公交网络站点数据爬取

步骤

运用Python的Requests及BeautifuiSoup模块进行静态网页的爬取,网页爬虫本质为两步: 
1、设置请求参数(url、headers、cookies、post或get验证等)访问目标站点的服务器; 
2、解析服务器返回的文档,提取需要的信息。

站点http://beijing.8684.cn/

一、环境配置

# -*- coding: utf-8 -*-
#导入requests
import requests
#导入bs4中的BeautifulSoup
from bs4 import 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'}
# 开始的URL地址
all_url = 'http://beijing.8684.cn' 
start_html = requests.get(all_url, headers=headers) 
# print(start_html.text)
# 以lxml的方式解析html文档
Soup = BeautifulSoup(start_html.text, 'lxml') 

二、站点分析

1)北京市公交 - 以数字开头 - “F12”启动开发者工具,点击“Elements”,点击“1”  - 发现链接保存在<div class="bus_kt_r1">里 - 提取出div里的href即可

all_a = Soup.find(‘div’,class_=’bus_kt_r1’).find_all(‘a’)

2)每路公交的链接都在<div id="con_site_1" class="site_list"> 的<a>里面 - 取出里面的herf即为线路网址,其内容即为线路名称

# 取出a标签的href属性
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') 

3)打开线路链接查看具体的站点信息 - 打开页面分析文档结构后发现 - 线路的基本信息存放在<div class="bus_i_content">里面 - 公交站点信息则存放在<div class="bus_line_top">及<div class="bus_line_site">里面

# 取出a1标签的文本
title1 = a2.get_text() 
# 取出a标签的href属性
href1 = a2['href'] 
#print (title1,href1)
# 构建线路站点url
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)
# 如果存在环线,也返回两个list,只是其中一个为空
if line_num==2:  
    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]

由此将某条线路的相关信息及上、下行站点信息都解析出来,如果想要爬取全市的公交网络站点,只需加入循环即可

  

猜你喜欢

转载自www.cnblogs.com/5211314jackrose/p/11307930.html