如何使用Regex和Python匹配文本文件中的Tumblr网址

麦克斯

我对regex还是很陌生,所以我尝试自己解决一段时间,但无法提出解决方案。(我正在尝试使用python 2.7做到这一点)

我有一个来自帖子和笔记的tumblr链接列表。他们看起来像

"http://TumblrUsername.tumblr.com/post/hello/notes/somemoresutff/464654" 

我要做的是仅选择“ http://TumblrUsername.tumblr.com/ ”部分,其余部分保留下来,以便我可以编制tumblr用户列表。

我的代码看起来像这样,但是我的问题是如何选择想要的东西...

import urllib
import requests
import lxml
from bs4 import BeautifulSoup


def find_notes():

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

    f = requests.get('http://fullthrottleauto.tumblr.com/post/132323884114/treunenthibault-ferrari-599xx-evo-as-i-love')

    soup = BeautifulSoup(f.text, "lxml")

    for post_note in soup.find_all('a', href=True):

        print post_note['href']
        returnline = str(post_note['href'])

        if '.tumblr.com/' in returnline:
           ## I need to do some thing here to extract "only the http://username.tumblr.com/"
            file.write(returnline + '\n')


find_notes()
伊斯兰教

下面的代码为什么工程正则表达式? It prints links and writes them into a file specified by the path!

import urllib
import requests
import lxml
from bs4 import BeautifulSoup


def find_notes():

    data_file = open(r"C:\Users\USER_NAME\Desktop\output.txt", 'ab')

    f = requests.get('http://fullthrottleauto.tumblr.com/post/132323884114/treunenthibault-ferrari-599xx-evo-as-i-love')

    soup = BeautifulSoup(f.text, "lxml")

    for post_note in soup.find_all('a', {'rel':'nofollow'}):
        if post_note['href'].endswith('.tumblr.com/') and post_note['href'].startswith('http') :
            print post_note['href']
            data_file.write(post_note['href']+'\n')
    data_file.close()


find_notes()

它打印-

http://jambo077.tumblr.com/
http://jambo077.tumblr.com/
http://thelordlux.tumblr.com/
http://thelordlux.tumblr.com/
http://dp0d.tumblr.com/
http://dp0d.tumblr.com/
http://fullthrottleauto.tumblr.com/
http://dp0d.tumblr.com/
http://dp0d.tumblr.com/
http://fraggreen.tumblr.com/
http://fraggreen.tumblr.com/
http://amazingcars.tumblr.com/
http://kennylayy.tumblr.com/
http://kennylayy.tumblr.com/
http://fullthrottleauto.tumblr.com/
http://coco2280.tumblr.com/
http://coco2280.tumblr.com/
http://fullthrottleauto.tumblr.com/
http://devrimdeniz3.tumblr.com/
http://devrimdeniz3.tumblr.com/
http://nicholasembly.tumblr.com/
http://nicholasembly.tumblr.com/
http://fullthrottleauto.tumblr.com/
http://nicholasembly.tumblr.com/
http://nicholasembly.tumblr.com/
http://geee22.tumblr.com/
http://geee22.tumblr.com/
http://donymadero.tumblr.com/
http://donymadero.tumblr.com/
http://avromen.tumblr.com/
http://avromen.tumblr.com/
http://carbonmotors.tumblr.com/
http://carbonmotors.tumblr.com/
http://blackdragonheartrider.tumblr.com/
http://blackdragonheartrider.tumblr.com/
http://travelerintheworldofdreams.tumblr.com/
http://travelerintheworldofdreams.tumblr.com/
http://evo-dreaming.tumblr.com/
http://evo-dreaming.tumblr.com/
http://fullthrottleauto.tumblr.com/
http://kareem121.tumblr.com/
http://kareem121.tumblr.com/
http://hotmenandhotcars.tumblr.com/
http://hotmenandhotcars.tumblr.com/
http://fullthrottleauto.tumblr.com/
http://schnixon.tumblr.com/
http://schnixon.tumblr.com/
http://fullthrottleauto.tumblr.com/
http://schnixon.tumblr.com/
http://schnixon.tumblr.com/
http://mikeawwr.tumblr.com/
http://mikeawwr.tumblr.com/
http://joshke1.tumblr.com/
http://joshke1.tumblr.com/
http://banginscrew.tumblr.com/
http://banginscrew.tumblr.com/
http://smiley-sj.tumblr.com/
http://smiley-sj.tumblr.com/
http://char1ie1000.tumblr.com/
http://char1ie1000.tumblr.com/
http://fullthrottleauto.tumblr.com/
http://char1ie1000.tumblr.com/
http://char1ie1000.tumblr.com/
http://relentless-haedons.tumblr.com/
http://relentless-haedons.tumblr.com/
http://metinpurde.tumblr.com/
http://metinpurde.tumblr.com/
http://superkingchris.tumblr.com/
http://superkingchris.tumblr.com/
http://16frango16.tumblr.com/
http://16frango16.tumblr.com/
http://franck-brevet.tumblr.com/
http://franck-brevet.tumblr.com/
http://car1ba.tumblr.com/
http://car1ba.tumblr.com/
http://trezio.tumblr.com/
http://trezio.tumblr.com/
http://molounhuevofrito.tumblr.com/
http://molounhuevofrito.tumblr.com/
http://fullthrottleauto.tumblr.com/
http://rebeccabum.tumblr.com/
http://rebeccabum.tumblr.com/
http://madv8.tumblr.com/
http://madv8.tumblr.com/
http://jrcs87lol.tumblr.com/
http://jrcs87lol.tumblr.com/
http://fullthrottleauto.tumblr.com/
http://jrcs87lol.tumblr.com/
http://jrcs87lol.tumblr.com/
http://alegasta.tumblr.com/
http://alegasta.tumblr.com/
http://ericj3love.tumblr.com/
http://ericj3love.tumblr.com/
http://frostfiree.tumblr.com/
http://frostfiree.tumblr.com/
http://bull58.tumblr.com/
http://bull58.tumblr.com/
http://fullthrottleauto.tumblr.com/
http://fumihirokoyama.tumblr.com/
http://fumihirokoyama.tumblr.com/
http://thethatnoelguysstuff.tumblr.com/
http://thethatnoelguysstuff.tumblr.com/
http://fullthrottleauto.tumblr.com/
http://thethatnoelguysstuff.tumblr.com/
http://thethatnoelguysstuff.tumblr.com/
http://peachedme.tumblr.com/
http://peachedme.tumblr.com/
http://il-salice-errante.tumblr.com/
http://il-salice-errante.tumblr.com/
http://fajhr.tumblr.com/
http://fajhr.tumblr.com/
http://jah-eras.tumblr.com/
http://jah-eras.tumblr.com/
http://fullthrottleauto.tumblr.com/

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在 Python 中匹配和从文本文件中提取

使用Regex查看Python中的文本文件

如何使用文本文件中的数字对python中的文本文件进行排序

如何使用python和regex合并两个文本文件

如何添加www。到文本文件中的网址

如何使用python查找文本文件中的行数?

如何使用python在终端中显示文本文件

如何使用 Pandas 在 Python 中读取文本文件

如何使用python编辑文本文件中的数据?

python中如何使用map和split解析文本文件?

如何使用Python在文本文件中读取和写入多个值?

使用 PowerShell,在文本文件中搜索多个匹配项和分组结果

如何从用户输入中搜索文本文件而不是使用python打印文本文件?

使用Bash,Perl和Regex将文本文件中的变量提取到数组中

如何使Python在文本文件中搜索并打印每条匹配的行?

如何使用 bash 命令删除文本文件中匹配多个模式的行?

在文本文件中搜索和替换-使用RegEx变量模式

使用Regex分隔文本文件中的单词

如何使用php查找和替换文本文件中的文本?

从 python 中的文本文件中检索行和

使用Python解析文本文件中的文本块

如何通过python regEx从文本文件中搜索href?

如何使用sed + regex在指定位置的文本文件中插入一行?

如何使用 python groupby 从给定的文本文件中拆分测试名称和日志详细信息

如何从文本文件重命名python中的文件

如何查找和替换模式“);” 在文本文件中?

如何从文本文件中读取和存储数据

如何从文本文件中读取和保存数据?

在python中读取和分组文本文件的内容