使用python在文本文件中仅保留某些行 [英] Keep only certain lines in a text file using python

查看:66
本文介绍了使用python在文本文件中仅保留某些行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个脚本,该脚本可让我从某些网站获取"solidfiles.com"链接.我有所有的href链接.但是,我无法仅使用python保留solidfiles.com链接.

I'm working on a script that lets me fetch the "solidfiles.com" links from certain website. I have got all the href links. But, I'm failing to keep only solidfiles.com links using python.

这是我正在尝试的网站从

这是我当前的脚本:-

import re
import requests
from bs4 import BeautifulSoup
import os
import fileinput

Link = 'https://animetosho.org/view/jacobswaggedup-kill-la-kill-bd-1280x720-mp4-batch.n677876'
q = requests.get(Link)
soup = BeautifulSoup(q.text)
#print soup
subtitles = soup.findAll('div',{'class':'links'})
#print subtitles


with  open("Anilinks.txt", "w") as f:
    for link in subtitles:
        x = link.find_all('a', limit=26)
        for a in x:
            url = a['href']
            f.write(url+'\n')

以此,我将所有链接写入了名为"Anilinks.txt"的文本文件中.我似乎无法仅保留solidfiles链接.任何提示都会很棒.

With this, I have written all the links in the text file named "Anilinks.txt". I can't seem to keep only solidfiles links. Any hint would be great.

推荐答案

这可能会起作用(如果您已经有一个.txt文件):

That will probably work (if you already have a .txt file):

# Store the links we need in a list
links_to_keep = []
with open("Anilinks.txt", "r") as f:

     for line in f.readlines():
         if 'solidfiles.com' in line:
             links_to_keep.append(line)

# Write all the links in our list to the file
with open("Anilinks.txt", "w") as f:

    for link in links_to_keep:
        f.write(link)


或者您可以在写入文件之前过滤链接,然后代码的最后一部分将如下所示:


Or you can filter the links before writing to file, then the last part of your code will look like this:

with  open("Anilinks.txt", "w") as f:
    for link in subtitles:
        x = link.find_all('a', limit=26)
        for a in x:
            if 'solidfiles.com' in a['href']:
                url = a['href']
                f.write(url+'\n')

这篇关于使用python在文本文件中仅保留某些行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆