【问题标题】:list of scraped elements showing output in different format以不同格式显示输出的抓取元素列表
【发布时间】:2020-03-31 18:36:44
【问题描述】:

我正在尝试使用 Python 中的 BeautifulSoup4 抓取特定地区的天气报告数据。 这是我的代码:

from bs4 import BeautifulSoup
import requests
import os
import sys


url = 'https://www.accuweather.com/en/in/guwahati/186893/weather-forecast/186893'
agent = {"User-Agent":'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'}
page = requests.get(url, headers=agent)
soup = BeautifulSoup(page.content, 'lxml') #= bs4 element
#print(soup.prettify())


#alldata is a tag of bs4 element
alldata = soup.find_all(class_='day-panel')


#This will give us all the required data we just need to arrange it nicely
datas = []
for h in alldata:
    datas.append(h.text.strip())

print(datas)
print(datas[0])

第一个打印语句显示输出为:

['Current Weather\n\t\n\n\t\t11:55 PM\n\t\n\n\n\n\t\t\t22°\n\t\t\n\n\t\t\t\tC\n\t\t\t\n\n\n\t\tRealFeel®\n\t\t20°\n\t\n\n\t\tPartly cloudy', 'Today\n\t\n\n\t\t3/31\n\t\n\n\n\n\t\t\t34°\n\t\t\n\n\t\t\t\tHi\n\t\t\t\n\n\n\t\tRealFeel®\n\t\t36°\n\t\n\n\t\tVery warm with hazy sunshine', 'Tonight\n\t\n\n\t\t3/31\n\t\n\n\n\n\t\t\t16°\n\t\t\n\n\t\t\t\tLo\n\t\t\t\n\n\n\t\tRealFeel®\n\t\t16°\n\t\n\n\t\tPatchy clouds', 'Tomorrow\n\t\n\n\t\t4/1\n\t\n\n\n\n\t\t\t36°\n\t\t\n\n\t\t\t\t/ 16°\n\t\t\t\n\n\n\t\tRealFeel®\n\t\t\n\t\n\n\t\tHot with hazy sunshine']

我只想要文本,而不是列表。
第二个打印语句显示输出为:

Current Weather


        11:56 PM




            22°


                C



        RealFeel®
        20°


        Mostly clear

预期输出:

'Current Weather\n\t\n\n\t\t11:55 PM\n\t\n\n\n\n\t\t\t22°\n\t\t\n\n\t\t\t\tC\n\t\t\t\n\n\n\t\tRealFeel®\n\t\t20°\n\t\n\n\t\tPartly cloudy'

我应该如何解决这个问题?

【问题讨论】:

    标签: python-3.x list web-scraping beautifulsoup


    【解决方案1】:

    这样打印的原因是 Python 正在为数据中的每个 \n\t 创建换行符和制表符。要在打印时忽略这些转义字符,请使用 Python repr 函数。

    像这样:

    print(repr(datas[0]))
    

    输出:

    'Current Weather\n\t\n\n\t\t12:28 AM\n\t\n\n\n\n\t\t\t71°\n\t\t\n\n\t\t\t\tF\n\t\t\t\n\n\n\t\tRealFeel®\n\t\t69°\n\t\n\n\t\tMostly clear'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-11
      • 2018-07-03
      • 1970-01-01
      • 2017-09-15
      • 2020-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多