【问题标题】:Python: Replacing url with titlePython:用标题替换 url
【发布时间】:2014-06-13 16:21:48
【问题描述】:

我编写了这段代码来用它们的标题替换 url。它确实会根据需要用标题替换 url,但它会在下一行打印它们的标题。

twfile.txt 包含以下几行:

link1 http://t.co/HvKkwR1c
no link line

输出tw2文件:

link1
Instagram
no link line

但我想以这种形式输出:

link1 Instagram
no link line

我该怎么办?

我的代码:

from bs4 import BeautifulSoup
import urllib

output = open('tw2file.txt','w')

with open('twfile.txt','r') as inputf:
    for line in inputf:
        try:
            list1 = line.split(' ')
            for i in range(len(list1)):

                if "http" in list1[i]:
                    ##print list1[i]
                    response = urllib.urlopen(list1[i])
                    html = response.read()
                    soup = BeautifulSoup(html)
                    list1[i] = soup.html.head.title
                    ##print list1[i]


                    list1[i] = ''.join(ch for ch in list1[i])
                else:
                    list1[i] = ''.join(ch for ch in list1[i])
            line = ' '.join(list1)
            print line
            output.write(line)
        except:
            pass


inputf.close()
output.close()

【问题讨论】:

    标签: python url python-2.7 beautifulsoup urllib


    【解决方案1】:

    试试这个代码:(见这里、这里和这里)

    from bs4 import BeautifulSoup
    import urllib
    
    with open('twfile.txt','r') as inputf, open('tw2file.txt','w') as output:
        for line in inputf:
            try:
                list1 = line.split(' ')
                for i in range(len(list1)):
                    if "http" in list1[i]:
                        response = urllib.urlopen(list1[i])
                        html = response.read()
                        soup = BeautifulSoup(html)
                        list1[i] = soup.html.head.title
                        list1[i] = ''.join(ch for ch in list1[i]).strip() # here
                    else:
                        list1[i] = ''.join(ch for ch in list1[i]).strip() # here
                line = ' '.join(list1)
                print line
                output.write('{}\n'.format(line))  # here
            except:
                pass
    

    顺便说一句,您使用的是 Python 2.7.x +,两个 opens 在同一个 with 子句中表示。他们的closes 也是不必要的。

    【讨论】:

      【解决方案2】:

      关于写入文件的内容

      fileobject = open("bar", 'w' )
      fileobject.write("Hello, World\n") # newline is inserted by '\n'
      fileobject.close()
      

      关于控制台输出

      print line 更改为print line,

      Python 在末尾写入 '\n' 字符,除非 print 语句以逗号结尾。

      【讨论】:

      • 不影响输出
      • 为什么要打印 2 次?打印行和 output.write(line)?
      • print 似乎是 console。另一个似乎是file
      猜你喜欢
      • 2015-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 2016-05-05
      • 2021-10-22
      相关资源
      最近更新 更多