python - 删除 Python URL 列表末尾的特殊字符/标点符号

标签 python regex python-3.x

我正在编写一个Python代码来从输入文件中提取所有URL,其中包含来自Twitter(推文)的内容或文本。然而,在这样做时,我意识到在 python 列表中提取的几个 URL 在末尾有“特殊字符”或“标点符号”,因此我无法进一步解析它们以获取基本 URL 链接。我的问题是:“如何识别并删除列表中每个网址末尾的特殊字符”?

当前输出:

['https://twitter.com/GVNyqWEu5u', 'https://twitter.com/GVNyqWEu5u'', 'https://twitter.com/GVNyqWEu5u@#', 'https://twitter.com/GVNyqWEu5u"']

期望的输出:

['https://twitter.com/GVNyqWEu5u', 'https://twitter.com/GVNyqWEu5u', 'https://twitter.com/GVNyqWEu5u', 'https://twitter.com/GVNyqWEu5u']

您会发现,并非“当前输出”列表中的所有元素在末尾都有特殊字符/标点符号。任务是仅从具有字符/标点符号的列表元素中识别和删除它们。

我使用以下正则表达式从推文文本中提取 Twitter URL:lst = re.findall('(http.?://[^\s]+)', text) 我可以在此步骤中删除 URL 末尾的特殊字符/标点符号吗?

完整代码:

import urllib.request, urllib.parse, urllib.error
from bs4 import BeautifulSoup
from socket import timeout
import ssl
import re
import csv

ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

count = 0
file = "Test.CSV"
with open(file,'r', encoding='utf-8') as f, open('output_themes_1.csv', 'w', newline='', encoding='utf-8') as ofile:
    next(f)
    reader = csv.reader(f)
    writer = csv.writer(ofile)
    fir = 'S.No.', 'Article_Id', 'Validity', 'Content', 'Geography', 'URL'
    writer.writerow(fir)
    for line in reader:
        count = count+1
        text = line[5]
        lst = re.findall('(http.?://[^\s]+)', text)
        if not lst:
            x = count, line[0], 'Empty List', text, line[8], line[6]
            print (x)
            writer.writerow(x)
        else:
            try:
                for url in lst:
                    try:
                        html = urllib.request.urlopen(url, context=ctx, timeout=60).read()
                        #html = urllib.request.urlopen(urllib.parse.quote(url, errors='ignore'), context=ctx).read()
                        soup = BeautifulSoup(html, 'html.parser')
                        title = soup.title.string
                        str_title = str (title)
                        if 'Twitter' in str_title:
                            if len(lst) > 1: break
                            else: continue
                        else:
                            y = count, line[0], 'Parsed', str_title, line[8], url
                            print (y)
                            writer.writerow(y)
                    except UnicodeEncodeError as e:
                        b_url = url.encode('ascii', errors='ignore')
                        n_url = b_url.decode("utf-8")
                        try:
                            html = urllib.request.urlopen(n_url, context=ctx, timeout=90).read()
                            soup = BeautifulSoup(html, 'html.parser')
                            title = soup.title.string
                            str_title = str (title)
                            if 'Twitter' in str_title:
                                if len(lst) > 1: break
                                else: continue
                            else:
                                z = count, line[0], 'Parsed_2', str_title, line[8], url
                                print (z)
                                writer.writerow(z)
                        except Exception as e:
                            a = count, line[0], str(e), text, line[8], url
                            print (a)
                            writer.writerow(a)
            except Exception as e:
                b = count, line[0], str(e), text, line[8], url
                print (b)
                writer.writerow(b)
print ('Total Rows Analyzed:', count)

最佳答案

假设特殊字符出现在您可以使用的字符串末尾:

mydata = ['https://twitter.com/GVNyqWEu5u', "https://twitter.com/GVNyqWEu5u'", 'https://twitter.com/GVNyqWEu5u@#', 'https://twitter.com/GVNyqWEu5u"']
mydata = [re.sub('[^a-zA-Z0-9]+$','',item) for item in mydata]
print(mydata)

打印:

['https://twitter.com/GVNyqWEu5u', 'https://twitter.com/GVNyqWEu5u', 'https://twitter.com/GVNyqWEu5u', 'https://twitter.com/GVNyqWEu5u']

关于python - 删除 Python URL 列表末尾的特殊字符/标点符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52118382/

相关文章:

javascript - 如何使用正则表达式获取此处的日期?

python - 在元素之间添加逗号

python - 如何使用列表理解从列表中返回元组和计数

regex - 具有多个选项的 MongoDB 正则表达式

ruby - 替换所有其他字符

Python:当网格大小太大时,遍历网格的递归调用计数方式会产生不正确的答案

python - 我如何使用 python 文件和 RopeVim 插件为 "Rope project root folder: . "指定什么?

javascript - 查找字符串中的重复项

python - 我如何使用 PRAW 列出 subreddit 中的热门评论?

python - 如何将 numpy 数组转换为(并显示)图像?