在for循环外检查lt = soup.select(‘.tb_railway_list’)是可以得到想要的 但是进行循环中的第一条时发现只能出来一条 小白 求问
import urllib.request
import urllib.parse
from bs4 import BeautifulSoup
import pandas as pd
class XieChengSpider(object):
url = 'https://bus.ctrip.com/busListn.html?'
def __init__(self,start_city,end_city,date):
#将上面的参数都保存为自己的成员属性
self.start_city = start_city
self.end_city = end_city
self.date = date
self.qiche = []
def handle_request(self,url_1):
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.5773.400 QQBrowser/10.2.2059.400',
}
request = urllib.request.Request(url=url_1,headers=headers)
return request
def run(self):
data = {
'from':self.start_city,
'to':self.end_city,
'date':self.date,
}
string = urllib.parse.urlencode(data)
url_1 = self.url + string
request=self.handle_request(url_1)
#发送请求,获取内容
content = urllib.request.urlopen(request).read().decode()
#解析内容
self.parse_content(content)
def parse_content(self,content):
#生成对象
soup = BeautifulSoup(content,'lxml')
lt = soup.select('.tb_railway_list')
for tr in lt:
lt_time = tr.select('.railway_time')[0].text
print(lt_time)
"""
lt_price = table.select('.railway_seat>.price_r>span')[0].string
lt_start = table.select('.icon_start')[0].next_sibling
lt_end = table.select('.icon_end')[0].next_sibling
lt_data = {
'出发时间':lt_time,
'价格':lt_price,
'出发车站':lt_start,
'到达车站':lt_end
}
self.qiche.append(lt_data)
print(self.qiche)
#df = pd.DataFrame(self.qiche)
#print(df)
"""
def main():
start_city = input('请输入出发城市:')
end_city = input('请输入到达城市:')
date = input('请输入出发日期:')
#创建对象,启动爬取程序
spider=XieChengSpider(start_city,end_city,date)
spider.run()
if name == ‘__main__’:
main()### 问题描述
问题出现的环境背景及自己尝试过哪些方法
相关代码
// 请把代码文本粘贴到下方(请勿用图片代替代码)
你期待的结果是什么?实际看到的错误信息又是什么?