我正在编写一个应用程序,它显示了去镇上的公共汽车的时间表。你能帮帮我吗?
这是我写的,但它不起作用(我只想要标题,或“时间”的巴士在网页上);
import requests
from bs4 import BeautifulSoup
def RuterBuss(max_pages):
page = 1
while page <= max_pages:
url = 'https://ruter.no/reiseplanlegger/Mellom/Fra/(2190085)
%C3%98ster%C3%A5s%20senter%20(B%C3%A6rum)/til/(3010200)Majorstuen
%20%5bT-bane%5d%20(Oslo)/etter/#st:0,sp:0,bp:0' + str(page)
source_code = requests.get(url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text, "html.parser")
for link in soup.findAll('span', {'class':'route-list__title ng-binding'}):
title = link.string
print(title)
page += 1
RuterBuss(1)其结果是:
Process finished with exit code 0就像我说的,我想要公共汽车开走时的时间。你能看到我忘记了什么或者做错了什么吗?
发布于 2016-12-13 22:32:37
因为你的输出是
Process finished with exit code 0基本上是因为您的代码没有到达任何print语句。
更详细地说,这意味着soup变量是while循环的第一个(也是唯一一个)迭代的空列表。
无论如何,我已经查看了您在脚本中使用的页面,我认为这是不可能达到您正在做的信息,因为页面是通过AngularJS呈现的。实际上,我试图以这种方式更改您的for循环:
for link in soup.findAll('strong'):因为我已经分析了源和想要的信息,所以可以在<strong>元素中获得这些信息,但是结果如下:
{{travel.routeName}}
{{travel.formattedDepartureTime}} – {{travel.formattedArrivalTime}}
{{deviation.heading}}
{{deviation.heading}}
{{remark.heading}}
{{deviation.heading}}我的另一个想法是在浏览器的开发工具(控制台)中搜索任何XHR或JS来获取所请求的信息,可能是通过json,但我什么也没找到。
https://stackoverflow.com/questions/41129943
复制相似问题