Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7703599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:29:46+00:00 2026-05-31T23:29:46+00:00

Scraping pages using BeautifulSoup; trying to filter out links that end in …html#comments Code

  • 0

Scraping pages using BeautifulSoup; trying to filter out links that end in “…html#comments”

Code follows:

import urllib.request
import re
from bs4 import BeautifulSoup

base_url = "http://voices.washingtonpost.com/thefix/morning-fix/"
soup = BeautifulSoup(urllib.request.urlopen(base_url)).findAll('a')
links_to_follow = []
for i in soup:
        if i.has_key('href') and \
    re.search(base_url, i['href']) and \
    len(i['href']) > len(base_url) and \
    re.search(r'[^(comments)]', i['href']):
        print(i['href'])

Python 3.2, Windows 7 64-bit.

The above script retains a link ending in “#comments”

I tried re.search([^comments], i['href']), re.search([^(comments)], i['href']) and re.search([^'comments'], i['href']) — all threw syntax errors.

New to Python, so apologies for banality.

I’m guessing either
(a) I don’t understand enough about the ‘r’ prefix to use it correctly or
(b) in response to [^(foo)] re.search returns not the set of lines that exclude ‘foo’, but the set of lines comprised of more than ‘foo’ alone. e.g., I keep my …#comments link because …texttexttext.html#comments precedes it or
(c) Python interprets “#” as a comment ending the line re.search is supposed to match.

I think I’m wrong on (b).

Sorry, know this is simple. Thanks,

Zack

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:29:48+00:00Added an answer on May 31, 2026 at 11:29 pm
    [^(comments)]
    

    means “one character that is neither a ( nor a c, an o, an m, an e, an n, a t, an s or a )“. Probably not what you intended.

    If your goal is to have a regex that only matches if the provided string does not end in #comments, then I would use

    ... and not re.search("#comments$", i['href'])
    

    or even better (why use a regex at all if it’s that simple?):

    ... and not i['href'].endswith("#comments")
    

    As for your other questions:

    The r'...' notation allows you to write “raw strings”, meaning that backslashes don’t need to be escaped:

    • r'\b' means “backslash + b” (which will be interpreted by the regex engine as “word boundary”
    • '\b' means “backspace character”
    • etc.

    # has no special meaning in a regex unless you use the (?x) or re.VERBOSE option. In that case, it does indeed start a comment in a multiline regex.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm scraping values from HTML pages using XPath inside of a java program to
I am scraping out the 'h2' and 'h3' tags from some html pages and
I am scraping HTML file using BeautifulSoup in python. I want to delete text
I am writing a screen scraping app that reads out various pages and extracts
I've been doing some HTML scraping in PHP using regular expressions. This works, but
I'm using BeautifulSoup to do some screen-scraping. My problem is this: I need to
I am experimenting with scraping certain pages from an RSS feed using curl and
I am scraping a website with HTML with php that retrieves a page and
So I'm writing an application that will do a little screen scraping. I'm using
I'm using Mechanize for scraping images url then I'm looking http://mechanize.rubyforge.org/Mechanize/Page/Image.html for to know

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.