python - 如何在 Web Scraping Python 中跳过没有元素的 URL
问题描述
我正在尝试抓取 14 个站点,但其中一些没有任何数据表,然后我想跳过这些 URL 并处理其余的。但是,我无法解决这个问题,我应该如何解决这个问题?
这是代码:
#define date ranges
dates_sep = np.arange(26,31,1)
#define hour range
h = np.arange(0,24,1)
h_list = list(h)
hours_L0 = [str(item).zfill(2) for item in h_list]
for d_sep in dates_sep:
for h_L0 in hours_L0:
urls_1 = "https://www.hko.gov.hk/en/wxinfo/rainfall/rf_record.shtml?form=rfrecorde&Selday=" + str(d_sep) + "&Selmonth=09&Selhour=" + str(h_L0)
html_content = requests.get(urls_1).text
soup = BeautifulSoup(html_content,"lxml")
问题从这里开始:
Rainfall = soup.find_all("table",
title="Table of the rainfall recorded in various regions")
#But some URLs don't have the elements mentioned above.
table1 = rainfall[0]
body = table1.find_all("tr")
head = body[0]
body_rows = body[1:]
headings = []
for item in head.find_all("td", align="center"):
item = (item.text).rstrip("\n")
headings.append(item)
all_rows = []
for row_num in range(len(body_rows)):
row = []
for row_item in body_rows[row_num].find_all("td"):
aa = re.sub("(\xa0)|(\n)|,","",row_item.text)
row.append(aa)
all_rows.append(row)
df = pd.DataFrame(data=all_rows)
df.head()
示例网址:
解决方案
推荐阅读
- angular - 如何从浏览器向 kong getway 上的 api 发出请求
- javascript - Select 2 v4.0.5 not working in bootstrap modal 4
- google-cloud-platform - 在 Google Cloud Platform 中使用 GKE 的多区域永久性磁盘
- d3.js - D3强制布局:旋转文字
- php - 使用 php 合并和更改 CSV 文件中的表名
- macos - macOS Catalina 上的 Vagrant 出现“过时文件句柄”错误
- mysql - mysqldump不写文件但没有错误
- python - 如何从 MS Windows 10 卸载 Django?
- java - 如何在 dao 类中制作通用接口
- bash - 在 TMUX 会话中修复 IEX 中的箭头键