使用Python将WebScraping转换为Excel

使用Python将WebScraping转换为Excel,python,web-scraping,Python,Web Scraping,这是我的原始代码 from bs4 import BeautifulSoup import requests import csv filename = open("test.csv",'w', newline='') writer = csv.writer(filename) URL = 'https://www.tadawul.com.sa/wps/portal/tadawul/markets/equities/market-watch/market-watch-t

这是我的原始代码

from bs4 import BeautifulSoup
import requests
import csv
 filename = open("test.csv",'w', newline='')
writer = csv.writer(filename)


URL = 'https://www.tadawul.com.sa/wps/portal/tadawul/markets/equities/market-watch/market-watch-today?locale=en'
header = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36'}
page = requests.get(URL)
soup = BeautifulSoup(page.text, 'lxml')


tbody=soup('table', id='table13')[0].find_all('tr')
  

for row in tbody:
  cols = row.findChildren(recursive=False)
  cols = [ele.text.strip() for ele in cols]
  writer.writerow(cols)
print(cols)

您需要特别处理不同的Colspan。我很想使用pandas read_html,然后整理感兴趣的数据框架。