问题遇到的现象和发生背景
Python 爬虫
如何将网站 https://www.bse.cn/nq/nqzqlistlgb.html 中地方政府债下显示的内容 和网站 https://www.bse.cn/nq/nqzqlistgb.html 中国债下显示的内容爬虫并保存到excel中?
遇到的现象和发生背景,请写出第一个错误信息
尝试用以下代码,但是生成的excel中只有第一行标题,没有内容
用代码块功能插入代码,请勿粘贴截图。 不用代码块回答率下降 50%
import re
import time
import datetime
import openpyxl
import parsel as parsel
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
url = 'https://www.bse.cn/nq/nqzqlistgb.html'
driver = webdriver.Chrome(executable_path='chromedriver.exe')
driver.get(url=url)
el_search = driver.find_element(by=By.XPATH, value='//*[@id="root"]/div[4]/div/div/div[1]/div[1]/div/div[1]')
# 点击
el_search.click()
time.sleep(3)
html_data = driver.page_source
#print(html_data)
selector = parsel.Selector(html_data)
results = selector.css('#root > div.mw-page > div > div > div.col-sm-12.col-md-9.col-md-push-3 > div.mw-box-content > div').getall()
#print(results)
#print(type(results))
#print(len(results))
len_results = len(results)
if len_results == 1 and results[0] == '':
print("no result")
else:
fieldnames = ['序号', '发行代码', '债券简称', '债券全称', '发行量(亿元)', '债券期限', '票面利率(%)', '计息方式', '起息日', '到期日']
work_book = openpyxl.Workbook()
work_book.create_sheet()
work_sheet = work_book.active
work_sheet.append(fieldnames)
for page in range(1, 5):
selector1 = parsel.Selector(driver.page_source)
results1 = selector1.css('#root > div.mw-page > div > div > div.col-sm-12.col-md-9.col-md-push-3 > div.mw-box-content > div').getall()
for r in results1[1:-1]:
row = re.findall(
'(.*?) (.*?) (.*?) (.*?) (.*?) (.*?) ('
'.*?) (.*?) (.*?) (.*?) (.*?) ',
r)[0]
print(list(row))
print(type(row))
work_sheet.append(list(row))
time.sleep(3)
if page < 5:
driver.find_element(by=By.LINK_TEXT, value='next').click()
time.sleep(5)
n = datetime.datetime.now().strftime('%m%d')
work_book.save(n + '_' + 'BJS_Code_Check' + '.xlsx')
print('file written successfully')