根据8684网爬取各市公交,按照公交路线名,运行时间,所经各站,票价,运营公司存于csv文件。非原创,原网页一时找不到,找到后补上。
使用到的三个库
import requests ##导入requests
from bs4 import BeautifulSoup ##导入bs4中的BeautifulSoup
import os
解析网址:路线查询分数字开头、汉字\字母开头、线路分类、热门分类。这里为了确保爬取所有路线选择了路线分类:bus_layer_r。但是热门分类对应的class也是bus_layer_r,所以不使用find_all,用Soup.find即可。
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://quanzhou.8684.cn' ##开始的URL地址
start_html = requests.get(all_url, headers=headers)
Soup = BeautifulSoup(start_html.text, 'lxml')
all_a = Soup.find('div',class_='bus_layer_r').find_all('a')
print(all_a)结果为[市区线路, 跨区线路, 大站快线, 晋江线路, 南安线路, 安溪线路, 惠安线路, 永春线路, 德化线路, 泉港线路]。下一步需要的是'/linex',不需要路线名称。
接下来分类爬取公交信息:按all_a中的'href'爬取每条线路中的公交
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('h1').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_label = Soup3.find('div',class_='bus_label')
if bus_label:
bus_length = bus_label.get_text()
else:
bus_length = []
#print(bus_name)#print (bus_name, bus_time, bus_cost,bus_company)
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')
#print(sites_x)
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: # 如果存在环线,也返回两个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=[],[]
#print(sites_x_list)
information = [bus_name,sites_x_list, bus_time, bus_cost,bus_company]
Network_list.append(information)
最后进行保存:直接保存在程序同一个文件夹中。
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()
# 输出处理后的数据
text_save(Network_list,'quanzhoubus.csv');
print ("finish")
不足:爬取时间太慢,存储数据格式需改进。