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

您的位置:首頁技術文章
文章詳情頁

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

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

問題描述

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

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()

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

問題解答

回答1:

寫csv文件簡單點 你的結構數據要成這樣 [['1. 東森新聞雲','新聞'],['2. 創世黎明(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’))

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 久治县| 九江县| 宁海县| 尤溪县| 遂昌县| 南丹县| 永济市| 金川县| 广西| 玉门市| 峨眉山市| 泰安市| 大石桥市| 涞源县| 新宁县| 柳河县| 姚安县| 荆门市| 特克斯县| 天门市| 连南| 南宫市| 射洪县| 阜新市| 林州市| 灵武市| 沁水县| 珠海市| 铜梁县| 天峻县| 宕昌县| 申扎县| 台湾省| 攀枝花市| 车险| 新晃| 铜川市| 廊坊市| 南和县| 望谟县| 永兴县|