如何使用python

时间:2015-07-07 12:57:58

标签: python python-2.7 hyperlink

我知道如何将多行写入文件,如果我知道要写多少行。但是,当我想写多行时会出现问题,但是,我不知道它们会有多少

我正在开发一个应用程序,该应用程序从网站上剪切并将结果的链接存储在文本文件中。但是,我们不知道它会回复多少行。我的代码现在如下。

r = requests.get('http://www.crunchyroll.com/i-cant-understand-what-my-husband-is-saying')
soup = BeautifulSoup(r.text)
print soup.title
subtitles = soup.findAll('div',{'class':'wrapper container-shadow hover-classes'})
for episode in subtitles:
  x = episode.find_all('a')
  for a in x:
   #print a['href']

   z = a['href']

  l = 'http://www.crunchyroll.com'+ z
  print l

这给了我想要的输出。所以,我试着通过添加:

在文件中写东西
file = open("BatchLinks.txt", "w")
file.write(l)
file.close()

但不幸的是,它只写了第一个链接。我怎样才能添加其他链接?

4 个答案:

答案 0 :(得分:3)

确保在for循环中写入链接。使用with命令可以保存以手动关闭文件。 这应该有效:

r = requests.get('http://www.crunchyroll.com/i-cant-understand-what-my-husband-is-saying')
soup = BeautifulSoup(r.text)
print soup.title
subtitles = soup.findAll('div',{'class':'wrapper container-shadow hover-classes'})

with open("BatchLinks.txt","w") as file: 

    for episode in subtitles:
        x = episode.find_all('a')
        for a in x:

            z = a['href']

            link = 'http://www.crunchyroll.com'+ z
            print link
            file.write(link)

答案 1 :(得分:2)

只需打开第一个文件,然后在迭代时写:

#include <fstream>
#include <set>

struct Person {
    std::string name;
    std::string nationality;
    std::set<std::string> hobbies;

    friend std::istream& operator>>(std::istream& is, Person& into) {
        size_t n = 0;
        if (getline(is, into.name) &&
            getline(is, into.nationality) &&
            is >> n && is.ignore(1024, '\n')) 
        {
            while (n--) {
                std::string hobby;
                if (getline(is, hobby))
                    into.hobbies.insert(hobby);
                else
                    is.setstate(std::ios::failbit);
            }
        }
        return is;
    }
};


#include <iostream>

int main() {
    std::ifstream ifs("input.txt");

    Person p;
    if (ifs >> p) {
        std::cout << "My information\n";
        std::cout << p.name << "\n";
        std::cout << p.nationality << "\n";
        std::cout << "I have " << p.hobbies.size() << " hobbies:\n";
        size_t counter = 0;
        for(auto const& hobby : p.hobbies) {
            std::cout  << ++counter << ". " << hobby << "\n";
        }
    } else { 
        std::cerr << "Parse failure\n";
    }
}

除非您想要一行中的所有链接,否则您需要在加入链接的末尾添加 with open(fa, "w") as f: r = requests.get('http://www.crunchyroll.com/i-cant-understand-what-my-husband-is-saying') soup = BeautifulSoup(r.text) print soup.title subtitles = soup.findAll('div', {'class': 'wrapper container-shadow hover-classes'}) for episode in subtitles: x = episode.find_all('a') for a in x: z = a['href'] f.write('http://www.crunchyroll.com{}\n'.format(z) ) 。您的代码也会编写最后一个链接而不是第一个链接。

输出:

\n

答案 2 :(得分:2)

以下代码应允许您将多行写入文件。

 with open(fa, "w") as f:
    r = requests.get('http://www.crunchyroll.com/i-cant-understand-what-my-  husband-is-saying')
    soup = BeautifulSoup(r.text)
    print soup.title
    subtitles = soup.findAll('div', {'class': 'wrapper container-shadow  hover-classes'})
    for episode in subtitles:
        x = episode.find_all('a')
        for a in x:
            z = a['href']
            f.write('http://www.crunchyroll.com{}\n'.format(z) )

答案 3 :(得分:1)

https://docs.python.org/3/library/functions.html#open

  

'w'打开写入,先截断文件

每次使用'w'模式打开文件时,都会截断它,这意味着丢弃任何现有内容。您可能希望在完成写入之前保持文件处于打开状态,然后将其关闭。