从文本文件中提取URL

时间:2015-07-02 15:51:07

标签: python regex url beautifulsoup extract

如何逐个提取我保存在文本文件中的链接,以便在我的其他代码中使用它: -

文本文件中链接的示例是: -

GridViewAdapter

我的文本文件中只有网址,而且一行中没有多个网址。

我必须使用链接的其他代码是: -

if(tableView == tableViewTopThreads){

            static NSString *simpleTableIdentifier = @"SimpleTableItem";

            ThreadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
            //cell.layer.shouldRasterize = YES;
            //cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
            if (cell == nil) {
                cell = [[ThreadTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
            }

            Thread *t = (Thread*)[tmpArray4 objectAtIndex:indexPath.row];
            cell.labelTitle.text = t.title;
            cell.labelCat.text = t.cat;
            cell.labelUser.text = [NSString stringWithFormat:@"%@ %@", t.firstname, t.lastname];
            cell.labelDate.text = [NSString stringWithFormat:@"%@", t.date];
            cell.labelCountry.text = t.country;
            cell.labelSubCat.text = t.subcat;
            cell.rating = t.rating;

            UIImage *imageStarDisabled = [UIImage imageNamed:@"star.png"];
            UIImage *imageStarEnabled = [UIImage imageNamed:@"star2.png"];

            cell.imageviewRating1.image = t.rating >= 1 ? imageStarEnabled : imageStarDisabled;
            cell.imageviewRating2.image = t.rating >= 2 ? imageStarEnabled : imageStarDisabled;
            cell.imageviewRating3.image = t.rating >= 3 ? imageStarEnabled : imageStarDisabled;
            cell.imageviewRating4.image = t.rating >= 4 ? imageStarEnabled : imageStarDisabled;
            cell.imageviewRating5.image = t.rating >= 5 ? imageStarEnabled : imageStarDisabled;

            [cell.contentView setNeedsUpdateConstraints];
            [cell.contentView updateConstraintsIfNeeded];
            [cell.contentView setNeedsLayout];
            [cell.contentView layoutIfNeeded];

            return cell;
            }

1 个答案:

答案 0 :(得分:1)

with open(file_name) as f:
    urls = f.readlines()

urls = ([s.strip('\n') for s in urls ])


for url in urls:
    # insert code here to do whatever you want with one url

这就是你想要的吗?这将读取每一行并将其存储到此列表中。

编辑以剥离' \ n'列表元素中的字符