爬虫(3)爬取数据再处理

上次我们爬取了1960年世界的GDP
但是还是有一些数据需要去除的,比如空,还有有空格的地方,还有广告位等等,这里我们去除这些东西

from selenium import webdriver
from bs4 import BeautifulSoup

driver=webdriver.Chrome()
url="https://www.kylc.com/stats/global/yearly/g_gdp/1960.html"
xpath="/html/body/div[2]/div[1]/div[5]/div[1]/div/div/div/table"
driver.get(url)
tablel=driver.find_element_by_xpath(xpath).get_attribute('innerHTML')
soup=BeautifulSoup(tablel,"html.parser")
table=soup.find_all('tr')
for row in table:
    cols=[col.text for col in row.find_all('td')]
    if len(cols)==0 or not cols[0].isdigit():
        continue
    print(cols)

这里加了
if len(cols)==0 or not cols[0].isdigit():
continue
目的是去除空,列表第一个元素是空格的行,还有广告位
结果如下
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_53029299/article/details/114850455