久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

csv - python多列存取爬蟲網(wǎng)頁(yè)?

瀏覽:113日期:2022-08-30 10:07:16

問題描述

爬蟲抓取的資料想分列存取在tsv上,試過很多方式都沒有辦法成功存存取成兩列資訊。想存取為數(shù)字爬取的資料一列,底下類型在第二列csv - python多列存取爬蟲網(wǎng)頁(yè)?

from urllib.request import urlopenfrom bs4 import BeautifulSoupimport reimport csvhtml = 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 strdef 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 strwith open(’0217.tsv’,’w’,newline=’’,encoding=’utf-8’) as f: f.write(GPname()) f.write(GPcompany())f.close()

可能對(duì)zip不熟悉,存取下來之后變成一個(gè)字一格也找到這篇參考,但怎么嘗試都沒有辦法成功https://segmentfault.com/q/10...

問題解答

回答1:

寫csv文件簡(jiǎn)單點(diǎn) 你的結(jié)構(gòu)數(shù)據(jù)要成這樣 [['1. 東森新聞雲(yún)','新聞'],['2. 創(chuàng)世黎明(Dawn of world)','遊戲']]

from urllib import urlopenfrom bs4 import BeautifulSoupimport reimport csvhtml = 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’))

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 沈丘县| 涡阳县| 孝感市| 和平区| 昔阳县| 武乡县| 邳州市| 万安县| 红原县| 增城市| 井研县| 云阳县| 广元市| 阿克陶县| 寿阳县| 天津市| 广平县| 潼关县| 洛扎县| 辰溪县| 天全县| 宜阳县| 神池县| 兴海县| 凤庆县| 辽阳县| 富川| 临沂市| 乐都县| 凉城县| 夏邑县| 城口县| 定西市| 昔阳县| 包头市| 河北省| 库尔勒市| 连山| 家居| 双江| 石嘴山市|