Web scraping 未打印到csv/xlsx文件的刮取数据

Web scraping 未打印到csv/xlsx文件的刮取数据,web-scraping,beautifulsoup,export-to-csv,xlsx,Web Scraping,Beautifulsoup,Export To Csv,Xlsx,我试图将数据刮取并存储在csv或xlsx文件中,但当我运行代码时,文件返回空 当我使用一个break在一个周期后停止迭代器时,我发现代码包含了我想要的一行数据。最终目标是将这些数据逐行写入文件。如果有助于了解,我正在使用beautifulsoup4 代码如下: 从bs4导入美化组 导入请求 导入xlsxwriter url='1〕https://www.rwaq.org/courses' response=requests.get(url) soup=BeautifulSoup(response

我试图将数据刮取并存储在csv或xlsx文件中,但当我运行代码时,文件返回空

当我使用一个
break
在一个周期后停止迭代器时,我发现代码包含了我想要的一行数据。最终目标是将这些数据逐行写入文件。如果有助于了解,我正在使用beautifulsoup4

代码如下:

从bs4导入美化组
导入请求
导入xlsxwriter
url='1〕https://www.rwaq.org/courses'
response=requests.get(url)
soup=BeautifulSoup(response.content'html.parser')
基地组织https://www.rwaq.org'
course_div=soup.find_all('div',attrs={'class':'course info'})
课程链接=[base+item.h3.a['href']用于课程div中的项目]
行=0
对于课程中的链接\u链接:
内部页面=requests.get(链接)
inner\u soup=BeautifulSoup(inner\u page.content,'html.parser')
课程名称=内部汤。查找('div',attrs={'class':'page title'})。h2.text
课程\讲师\姓名=内部\汤。查找('div',attrs={'class':'讲师详细信息'})。a.text.strip()
course_desc=inner_soup.find('div',attrs={'class':'teaching_desc'}).p.text.strip()
如果是内部的,请选择一个(“#组织部门课程内容部门:第n个子(4)部门行-流体部门”):
course_manhag=内部汤。选择其中一个(“#组织部门课程内容部门:第n个孩子(4)部门行液体部门”)。文本
elif inner_soup.选择_one(“#组织div.course-content div:nth child(4)div.row-fluid p”):
course_manhag=内部汤。选择一个(“#组织部门课程内容部门:第n个子(4)部门行流体p”)。文本
其他:
课程_manhag=“”
如果是内部的,请选择其中一个(“#组织部门课程内容部门:第n个子部门(5)部门行流体部门”):
课程要求=内汤。选择一个(
“#组织部门课程内容部门:第n个孩子(5)部门行-流体ul”)。文本
elif inner_soup.选择_one(“#组织部门课程内容部门:第n个子部门(5)部门行流体p”):
课程需求=内部课程。选择一个(“#组织部门课程内容部门:第n个子(5)部门行流体p”)。文本
其他:
课程要求=“”
如果是内部的,请选择一个(“#组织部门课程内容部门:第n个子(6)部门行-流体部门”):
课程输出=内部汤。选择一个(“#组织部门课程内容部门:第n个孩子(6)部门行液体部门”)。文本
elif inner_soup.选择_one(“#组织部门课程内容部门:第n个子(6)部门行流体p”):
课程输出=内部课程。选择一个(“#组织部门课程内容部门:第n个子(6)部门行流体p”)。文本
其他:
课程输出=“”
课程公司=内部汤。选择一个(
“主体部分容器-流体部分主题-封面部分封面-信息部分主题-组织部分PA”)。文本
课程日期来源=内部汤。选择一个('p.subject-date')。text.strip()[3:16]
如果“内部”选项“选择一个”(“p.subject-date”)为真:
课程日期到=内部汤。选择一个('p.subject-date')。text.strip()[31:]
其他:
课程日期至=“”
课程状态=内部汤。选择一个('p.subject-date span')。文本
课程_讲师_链接=[base+li.a['href']
查找所有(div,attrs={'class':'讲师详细信息'})]
课程iframe=内部汤。选择一个('iframe')。属性[“src”]
course_promo_link=course_iframe[:24]+'watch?v='+course_iframe[30:course_iframe.find('?')]
wb=xlsxwriter.Workbook('file001.xlsx')
工作表=wb.add_工作表()
填写(第0行,课程宣传链接)
工作表。写入第行(第1行,课程链接)
书写表(第2行,课程描述)
填写(第3行,课程结束)
填写(第4行,课程状态)
填写(第5行,课程名称)
填写(第6行,课程日期自)
填写(第7行,课程日期至)
填写表格(第8行,课程号)
填写(第9行,课程要求)
行+=1
wb.close()
打破

运行您的代码时会出现错误,因此无法进行测试。但我首先要尝试的是将
wb=xlsxwriter.Workbook('file001.xlsx')
wb.close()
for
循环中取出。我最初的想法是你每次都写得太多了。比如:

from bs4 import BeautifulSoup
import requests
import xlsxwriter

url = 'https://www.rwaq.org/courses'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
base = 'https://www.rwaq.org'

course_div = soup.find_all('div', attrs={'class': 'course-info'})
course_links = [base + item.h3.a['href'] for item in course_div]

#Initialize your file/workbook
wb = xlsxwriter.Workbook('C:/file001.xlsx')
sheet = wb.add_worksheet()

row = 0
for link in course_links:
    inner_page = requests.get(link)
    inner_soup = BeautifulSoup(inner_page.content, 'html.parser')
    course_name = inner_soup.find('div', attrs={'class': 'page-title'}).h2.text
    course_lecturer_name = inner_soup.find('div', attrs={'class': 'instructor-details'}).a.text.strip()
    course_desc = inner_soup.find('div', attrs={'class': 'lecture_desc'}).p.text.strip()
    if inner_soup.select_one('#organization div.course-content div:nth-child(4) div.row-fluid ul'):
        course_manhag = inner_soup.select_one('#organization div.course-content div:nth-child(4) div.row-fluid ul').text
    elif inner_soup.select_one('#organization div.course-content div:nth-child(4) div.row-fluid p'):
        course_manhag = inner_soup.select_one('#organization div.course-content div:nth-child(4) div.row-fluid p').text
    else:
        course_manhag = ''

    if inner_soup.select_one('#organization div.course-content div:nth-child(5) div.row-fluid ul'):
        course_require = inner_soup.select_one(
            '#organization div.course-content div:nth-child(5) div.row-fluid ul').text
    elif inner_soup.select_one('#organization div.course-content div:nth-child(5) div.row-fluid p'):
        course_require = inner_soup.select_one('#organization div.course-content div:nth-child(5) div.row-fluid p').text
    else:
        course_require = ''

    if inner_soup.select_one('#organization div.course-content div:nth-child(6) div.row-fluid ul'):
        course_out = inner_soup.select_one('#organization div.course-content div:nth-child(6) div.row-fluid ul').text
    elif inner_soup.select_one('#organization div.course-content div:nth-child(6) div.row-fluid p'):
        course_out = inner_soup.select_one('#organization div.course-content div:nth-child(6) div.row-fluid p').text
    else:
        course_out = ''

    course_company = inner_soup.select_one(
        'body div.container-fluid div div.subject-cover div.cover-info div div.subject-organization p a').text
    course_date_from = inner_soup.select_one('p.subject-date').text.strip()[3:16]
    if inner_soup.select_one('p.subject-date') is True:
        course_date_to = inner_soup.select_one('p.subject-date').text.strip()[31:]
    else:
        course_date_to = ''
    course_status = inner_soup.select_one('p.subject-date span').text
    course_lecturer_link = [base + li.a['href'] for li in
                            inner_soup.find_all("div", attrs={'class': 'instructor-details'})]
    course_iframe = inner_soup.select_one('iframe').attrs["src"]
    course_promo_link = course_iframe[:24] + 'watch?v=' + course_iframe[30:course_iframe.find('?')]

    sheet.write(row, 0, course_promo_link)
    sheet.write(row, 1, course_lecturer_link)
    sheet.write(row, 2, course_desc)
    sheet.write(row, 3, course_out)
    sheet.write(row, 4, course_status)
    sheet.write(row, 5, course_name)
    sheet.write(row, 6, course_date_from)
    sheet.write(row, 7, course_date_to)
    sheet.write(row, 8, course_manhag)
    sheet.write(row, 9, course_require)
    row += 1

# Close it once all rows are written    
wb.close()

是的,我想我正在这样做,我会尝试你的代码我希望它能工作谢谢@chitown88