美文网首页Python爬虫作业
二期python爬虫作业写入CSV

二期python爬虫作业写入CSV

作者: bill666500 | 来源:发表于2017-06-04 15:59 被阅读169次

    这次作业主要练习写入操作,普通基础的爬虫工作做好之后, 可以进行数据分析等等工作,所以爬虫好的数据就要进行写入操作。直接以糗事百科的爬虫当拿手的例子。Talk is cheap, talk is cheap show me the code.

    import requests
    import csv
    from bs4 import BeautifulSoup
    
    html = requests.get('http://www.qiushibaike.com/text/').content
    soup = BeautifulSoup(html,'lxml')
    links = soup.find_all('div',class_='content')
    
    csvfile = open("qiushi.csv","a") 
    writer = csv.writer(csvfile)
    for link in links:
     print(link.span.get_text())
     writer.writerow([link.span.get_text()])
    

    相关文章

      网友评论

        本文标题:二期python爬虫作业写入CSV

        本文链接:https://www.haomeiwen.com/subject/clovfxtx.html