gpt4 book ai didi

python - BeautifulSoup 获取给定标签后的所有链接

转载 作者:太空宇宙 更新时间:2023-11-03 15:23:38 26 4
gpt4 key购买 nike

我尝试使用 BeautifulSoup 抓取以下页面(例如 12 ),以获取从曼谷的一个地方到另一个地方的操作列表。

基本上,我可以如下查询和选择行程的描述。

url = 'http://www.transitbangkok.com/showBestRoute.php?from=Sutthawat+-+Arun+Amarin+Intersection&to=Sukhumvit&originSelected=true&destinationSelected=true&lang=en'
route_request = requests.get(url)
soup_route = BeautifulSoup(route_request.content, 'lxml')
descriptions = soup_route.find('div', attrs={'id': 'routeDescription'})

descriptions 的 HTML 如下所示

<div id="routeDescription">
...
<br/>
<img src="/images/walk_icon_small.PNG" style="vertical-align:middle;padding-right: 10px;margin-right: 0px;"/>Walk by foot to <b>Sanam Luang</b>
<br/>
<img src="/images/bus_icon_semi_small.gif" style="vertical-align:middle;padding-right: 10px;margin-right: 0px;"/>Travel to <b>Khok Wua</b> using the line(s): <b><a href="lines/bangkok-bus-line/2">2</a></b> or <a href="lines/bangkok-bus-line/15">15</a> or <a href="lines/bangkok-bus-line/44">44</a> or <a href="lines/bangkok-bus-line/47">47</a> or <a href="lines/bangkok-bus-line/59">59</a> or <a href="lines/bangkok-bus-line/201">201</a> or <a href="lines/bangkok-bus-line/203">203</a> or <a href="lines/bangkok-bus-line/512">512</a><br/>
...
</div>

基本上,我尝试获取前往下一个位置的操作和公交线路列表(问题已更新答案,但仍然没有解决)。

route_descrtions = []
for description in descriptions.find_all('img'):
action = description.next_sibling
to_station = action.next_sibling
n = action.find_next_siblings('a')
if 'travel' in action.lower():
lines = [to_station.find_next('b').text] + [a.contents[0] for a in n]
else:
lines = []
desp = {'action': action,
'to': to_station.text,
'lines': lines}
route_descrtions.append(desp)

但是,我不知道如何在每个操作(Travel to 操作)之后循环链接并附加到我的列表。我尝试了 find_next('a')find_next_siblings('a') 但没有完成我的任务。

输出

[{'action': 'Walk by foot to ', 'lines': [], 'to': 'Wang Lang (Siriraj)'},
{'action': 'Travel to ',
'lines': ['Chao Phraya Express Boat', '40', '48', '501', '508'],
'to': 'Si Phraya'},
{'action': 'Walk by foot to ', 'lines': [], 'to': 'Sheraton Royal Orchid'},
{'action': 'Travel to ',
'lines': ['16', '40', '48', '501', '508'],
'to': 'Siam'},
{'action': 'Travel to ',
'lines': ['BTS - Sukhumvit', '40', '48', '501', '508'],
'to': 'Asok'},
{'action': 'Walk by foot to ', 'lines': [], 'to': 'Sukhumvit'}]

所需输出

[{'action': 'Walk by foot to ', 'lines': [], 'to': 'Wang Lang (Siriraj)'},
{'action': 'Travel to ',
'lines': ['Chao Phraya Express Boat'],
...

最佳答案

以下应该有效:

from bs4 import BeautifulSoup
import requests
import pprint

url = 'http://www.transitbangkok.com/showBestRoute.php?from=Sutthawat+-+Arun+Amarin+Intersection&to=Sukhumvit&originSelected=true&destinationSelected=true&lang=en'
route_request = requests.get(url)
soup_route = BeautifulSoup(route_request.content, 'lxml')
routes = soup_route.find('div', attrs={'id': 'routeDescription'})

parsed_routes = list()
for img in routes.find_all('img'):
action = img.next_sibling
to_station = action.next_sibling
links = list()
for sibling in img.next_siblings:
if sibling.name == 'a':
links.append(sibling)
elif sibling.name == 'img':
break

lines = list()
if 'travel' in action.lower():
lines.extend([to_station.find_next('b').text])
lines.extend([link.contents[0] for link in links])

parsed_route = {'action': action, 'to': to_station.text, 'lines': lines}
parsed_routes.append(parsed_route)

pprint.pprint(parsed_routes)

输出:

[{'action': 'Walk by foot to ', 'lines': [], 'to': 'Wang Lang (Siriraj)'},
{'action': 'Travel to ',
'lines': ['Chao Phraya Express Boat'],
'to': 'Si Phraya'},
{'action': 'Walk by foot to ', 'lines': [], 'to': 'Sheraton Royal Orchid'},
{'action': 'Travel to ', 'lines': ['16'], 'to': 'Siam'},
{'action': 'Travel to ',
'lines': ['BTS - Sukhumvit', '40', '48', '501', '508'],
'to': 'Asok'},
{'action': 'Walk by foot to ', 'lines': [], 'to': 'Sukhumvit'}]

您的关键问题是n = action.find_next_siblings('a'),因为它在“当前”图像之后获得了同一级别的所有链接。由于所有图像和所有链接都处于同一级别,这不是您想要的。

您可能正在将图像视为链接的父节点。像这样的东西:

  • img1
    • 链接1
  • img2
    • 链接2
  • img3
    • 链接3
    • 链接4
    • 链接5

然而,实际上它更像是这样的:

  • img1
  • 链接1
  • img2
  • 链接2
  • img3
  • 链接3
  • 链接4
  • 链接5

当您请求图像时,您会得到 img1、img2 和 img3(在本例中)。当您询问所有下一个链接 sibling 时,您就得到了。因此,如果您在 img2 上,并要求 sibling 提供下一个链接,您就会得到它们,即,

  • img1
  • 链接1
  • img2 <你在这里,并且得到了...
  • 链接2 <这个,
  • img3 -(不是这个,因为它不是链接)
  • link3 <这个,
  • link4 <这个,以及
  • 链接5 <这个

我希望这能解释。我所做的更改是循环播放,直到找到图像并停在那里。因此,您的外部图像循环从那里继续。我还清理了一些代码。只是为了清楚起见。

关于python - BeautifulSoup 获取给定标签后的所有链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43302864/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com