csv - python多列存取爬蟲網頁?

 男孩形式恋人 发布于 2022-10-25 17:03

爬虫抓取的资料想分列存取在tsv上,试过很多方式都没有办法成功存存取成两列资讯。
想存取为数字爬取的资料一列,底下类型在第二列

from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
import csv

html = urlopen("http://www.app12345.com/?area=tw&store=Apple%20Store")
bs0bj = BeautifulSoup (html)


def GPname():
    GPnameList = bs0bj.find_all("dd",{"class":re.compile("ddappname")})
    str = ''
    for name in GPnameList:
        str += name.get_text()
        str += '\n'
        print(name.get_text())

    return str


def GPcompany():

    GPcompanyname = bs0bj.find_all("dd",{"style":re.compile("color")})
    str = ''
    for cpa in GPcompanyname:
        str += cpa.get_text()
        str += '\n'
        print(cpa.get_text())
    return str




with open('0217.tsv','w',newline='',encoding='utf-8') as f:
    f.write(GPname())
    f.write(GPcompany())

f.close()

可能对zip不熟悉,存取下来之后变成一个字一格
也找到这篇参考,但怎么尝试都没有办法成功
https://segmentfault.com/q/10...

1 个回答
  • 写csv文件简单点 你的结构数据要成这样 [["1. 東森新聞雲","新聞"],["2. 創世黎明(Dawn of world)","遊戲"]]

    from urllib import urlopen
    from bs4 import BeautifulSoup
    import re
    import csv
    
    html = urlopen("http://www.app12345.com/?area=tw&store=Apple%20Store")
    bs0bj = BeautifulSoup (html)
    GPnameList = [name.get_text() for name in bs0bj.find_all("dd",{"class":re.compile("ddappname")})]
    GPcompanyname = [cpa.get_text() for cpa in bs0bj.find_all("dd",{"style":re.compile("color")})]
    
    data = '\n'.join([','.join(d) for d in zip(GPnameList, GPcompanyname)])
    with open('C:/Users/sa/Desktop/0217.csv','wb') as f:
         f.write(data.encode('utf-8'))
    2022-10-26 23:34 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有