gpt4 book ai didi

Python Web Scraper 打印问题

转载 作者:行者123 更新时间:2023-12-01 03:42:49 26 4
gpt4 key购买 nike

我已经用Python创建了一个网络爬虫,但是在最后打印时我想打印我已经下载的(“Bakerloo:”+ info_from_website),正如你在代码中看到的那样,但它总是像info_from_website和忽略“Bakerloo:”字符串。无论如何都找不到解决办法。

import urllib
import urllib.request
from bs4 import BeautifulSoup
import sys

url = 'https://tfl.gov.uk/tube-dlr-overground/status/'
page = urllib.request.urlopen(url)
soup = BeautifulSoup(page,"html.parser")

try:
bakerlooInfo = (soup.find('li',{"class":"rainbow-list-item bakerloo "}).find_all('span')[2].text)
except:
bakerlooInfo = (soup.find('li',{"class":"rainbow-list-item bakerloo disrupted expandable "}).find_all('span')[2].text)

bakerloo = bakerlooInfo.replace('\n','')
print("Bakerloo : " + bakerloo)

最佳答案

我会使用 CSS selector相反,使用 disruption-summary 类获取元素:

import requests
from bs4 import BeautifulSoup

url = 'https://tfl.gov.uk/tube-dlr-overground/status/'
page = requests.get(url)
soup = BeautifulSoup(page.content, "html.parser")

service = soup.select_one('li.bakerloo .disruption-summary').get_text(strip=True)
print("Bakerloo: " + service)

打印:

Bakerloo: Good service

(此处使用requests)。

<小时/>

请注意,如果您只想列出所有电台及其中断摘要,请执行以下操作:

import requests
from bs4 import BeautifulSoup

url = 'https://tfl.gov.uk/tube-dlr-overground/status/'
page = requests.get(url)
soup = BeautifulSoup(page.content, "html.parser")

for station in soup.select("#rainbow-list-tube-dlr-overground-tflrail-tram ul li"):
station_name = station.select_one(".service-name").get_text(strip=True)
service_info = station.select_one(".disruption-summary").get_text(strip=True)

print(station_name + ": " + service_info)

打印:

Bakerloo: Good service
Central: Good service
Circle: Good service
District: Good service
Hammersmith & City: Good service
Jubilee: Good service
Metropolitan: Good service
Northern: Good service
Piccadilly: Good service
Victoria: Good service
Waterloo & City: Good service
London Overground: Good service
TfL Rail: Good service
DLR: Good service
Tram: Good service

关于Python Web Scraper 打印问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39295642/

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