使用BeautifulSoup提取HTML数据报错:运行返回空结果求助
帮你排查提取时间返回空的问题
首先先看看你提到的页面区域截图:
你提供的代码如下:
import qgrid import webbrowser import requests from bs4 import BeautifulSoup page = requests.get('http://www.meteo.gr/cf.cfm?city_id=14') #sending the request to take the html file. soup = BeautifulSoup(page.content, 'html.parser') #creating beautifulSoup object of the html code. four_days = soup.find(id="prognoseis")#PINPOINTING to the section that i want to focus (the outer). #Selecting specific elements , having as my base the seven_day. periods = [p.get_text() for p in four_days.select(".perhour-rowmargin .innerTableCell-fulltime")] #creating a Data Frame via pandas to print it TABLE-like. import pandas as pd weather = pd.DataFrame({"period ": periods}) print weather
我帮你梳理几个可能导致返回空结果的原因和解决办法:
选择器匹配不到元素:这是最常见的原因。网站的HTML结构可能和你参考的教程时期不一样了,
.perhour-rowmargin或者.innerTableCell-fulltime这些类名可能已经被修改。你可以先打印four_days的内容,看看里面的实际结构:print(four_days.prettify())然后在输出里找到时间对应的元素,调整选择器。比如如果时间元素直接在
four_days下的.innerTableCell-fulltime里,去掉前面的.perhour-rowmargin试试:periods = [p.get_text(strip=True) for p in four_days.select(".innerTableCell-fulltime")](加上
strip=True还能去掉多余的空格换行,让结果更整洁)Python语法错误:你的代码最后一行
print weather是Python2的语法,Python3里需要写成print(weather),这个错误可能会导致代码运行报错,先修正这个基础问题。页面动态加载的可能:如果
prognoseis区域的内容是通过JavaScript动态渲染的,那么requests获取的静态HTML里不会包含这些内容。这种情况下你需要用selenium来模拟浏览器加载页面,再提取内容。不过先优先排查前两个问题,大部分静态页面的问题都能通过调整选择器解决。
你可以先试试打印four_days的结构,确认元素的实际类名,再调整选择器,应该就能拿到想要的时间数据啦~
内容的提问来源于stack exchange,提问作者GioZio




