跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • 新人求解: Python 写入 txt 时数据丢失的问题
未分類
3 4 月 2020

新人求解: Python 写入 txt 时数据丢失的问题

新人求解: Python 写入 txt 时数据丢失的问题

資深大佬 : chaneyccy 55

主要问题在这:

for txt in list_: txts = txt.get_text() #在这里 print(txts), 结果还是完整的 download_run(title1, title2, title3, title4, txts) #在这里 print(txts), 数据只剩最后一个段落了

代码如下: def download(href_urls): for url in href_urls: mod_titles = [] ses = requests.session() html = ses.get(url, headers = header(), verify = False) soup = BeautifulSoup(html.content, ‘html.parser’) title_list = soup.find(class_ = ‘g-ctnBar’).find_all(‘a’) title1 = title_list[2].get_text() title2 = title_list[3].get_text() title3 = title_list[4].get_text() title4 = title_list[5].get_text() list_ = soup.find_all(‘div’, class_ = ‘detail-mod J_floor’)[:-3] for txt in list_: txts = txt.get_text() download_run(title1, title2, title3, title4, txts)

def download_run(title1, title2, title3, title4, txts): path = ‘C:/Users/Desktop/run/%s/%s/%s’ %(title1, title2, title3) if not os.path.exists(path): os.makedirs(path) with open(‘C:/Users/Desktop/run/%s/%s/%s/%s.txt’ %(title1, title2, title3, title4), ‘w’)as f: f.write(txts)

大佬有話說 (18)

  • 主 資深大佬 : chaneyccy

    排版有点乱,更新一下

    def download(href_urls):

    for url in href_urls:

    mod_titles = []

    ses = requests.session()

    html = ses.get(url, headers = header(), verify = False)

    soup = BeautifulSoup(html.content, ‘html.parser’)

    title_list = soup.find(class_ = ‘g-ctnBar’).find_all(‘a’)

    title1 = title_list[2].get_text()

    title2 = title_list[3].get_text()

    title3 = title_list[4].get_text()

    title4 = title_list[5].get_text()

    list_ = soup.find_all(‘div’, class_ = ‘detail-mod J_floor’)[:-3]

    for txt in list_:

    txts = txt.get_text()

    download_run(title1, title2, title3, title4, txts)

    def download_run(title1, title2, title3, title4, txts):

    path = ‘C:/Users/Desktop/run/%s/%s/%s’ %(title1, title2, title3)

    if not os.path.exists(path):

    os.makedirs(path)

    with open(‘C:/Users/Desktop/run/%s/%s/%s/%s.txt’ %(title1, title2, title3, title4), ‘w’)as f:

    f.write(txts)

  • 資深大佬 : JCZ2MkKb5S8ZX9pq

    “`
    你的代码
    “`

    这样可以保留模式。回复时无效。详情可查 markdown 格式。

  • 資深大佬 : JCZ2MkKb5S8ZX9pq

    格式

  • 主 資深大佬 : chaneyccy

    @JCZ2MkKb5S8ZX9pq 好的,平时没有用 markdown 写内容的习惯~ 我去研究下

  • 資深大佬 : cxyfreedom

    你遍历循环又每次写入,你循环完成后,txts 本来就只有一部分的数据,写入到文件中当然就只有最后一部分。

  • 資深大佬 : Vegetable

    https://www.runoob.com/python/python-func-open.html
    看表格

  • 資深大佬 : Vegetable

    @cxyfreedom #5 代码我读了,是因为每次都 open(file,’w’)写入的原因。

  • 資深大佬 : Wuuuu

    感觉应该是 txts = txts+”n”+txt.get_txt()?

  • 資深大佬 : Wuuuu

    @Vegetable py 靠缩进……这样的不知道到底是
    for txt in list_:

    txts = txt.get_text()

    download_run(title1, title2, title3, title4, txts)

    还是
    for txt in list_:

    txts = txt.get_text()

    download_run(title1, title2, title3, title4, txts)

    但大概率是第二种写法吧。

  • 資深大佬 : Wuuuu

    for txt in list_:

    t txts = txt.get_text()

    t download_run(title1, title2, title3, title4, txts)

    for txt in list_:

    t txts = txt.get_text()

    download_run(title1, title2, title3, title4, txts)

  • 資深大佬 : LiuSha

    把 w 改成 a

  • 資深大佬 : zhejiangblue

    打开模式用 a,w 会重新创建文件的

  • 主 資深大佬 : chaneyccy

    @Vegetable 感谢感谢,之前把从’a’改成’w’了,没考虑到这个原因,调回来正常了

  • 資深大佬 : cxyfreedom

    @Vegetable 如果 for 循环下面的话那就是这个问题。追加写入不能用 w。按这个代码逻辑不如一次性写入呢

  • 資深大佬 : vincexu

    ![image.png]( https://i.loli.net/2020/01/14/HBUCoWMXdINqpzF.png)

  • 資深大佬 : JCZ2MkKb5S8ZX9pq

    如果是 aw 的问题 建议拼接 txt 然后 w
    a 万一重复执行会重复写入

  • 資深大佬 : cherbim

    你文件写入的是 w,每次都会重写,用 a 追加啊

  • 資深大佬 : shunfa52000

    上次我程序结尾没有加 f.close,结果循环次数多的时候报错了

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具