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 8996093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:42:27+00:00 2026-06-15T23:42:27+00:00

I wrote the line below: [x[‘href’] for x in BeautifulSoup(data, parseOnlyThese=SoupStrainer(‘a’))] The data is

  • 0

I wrote the line below:

[x['href'] for x in BeautifulSoup(data, parseOnlyThese=SoupStrainer('a'))]

The data is achieved by urllib.urlopen(XXX).read() in python2.7.

It works well when the XXX is a page that consists of total English characters, such as http://python.org. But when it goes for a page there is some Chinese characters, it fails.
There will be a KeyError. And [x for ...] returns an empty list.

What’s more, if there is no parseOnlyThese=SoupStrainer('a'), it is OK for both.

Is there some bug of SoupStrainer?

from BeautifulSoup import BeautifulSoup, SoupStrainer
import urllib

data = urllib.urlopen('http://tudou.com').read()
[x['href'] for x in BeautifulSoup(data, parseOnlyThese=SoupStrainer('a'))]

gives the traceback:

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    [x['href'] for x in BeautifulSoup(data, parseOnlyThese=SoupStrainer('a'))]
  File "F:\ActivePython27\lib\site-packages\beautifulsoup-3.2.1-py2.7.egg\BeautifulSoup‌​.py", line 613, in __getitem__
    return self._getAttrMap()[key]
KeyError: 'href' 
  • 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-06-15T23:42:28+00:00Added an answer on June 15, 2026 at 11:42 pm

    There are <a> links on that page that do not have a href attribute. Use the following instead:

    [x['href'] for x in BeautifulSoup(data, parseOnlyThese=SoupStrainer('a')) if x.has_key('href')]
    

    For example, it is perfectly normal to declare a link target with <a name="something" />; you are selecting those tags too, but they do not have a href attribute and your code fails on that.

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

Sidebar

Related Questions

I wrote the program below in order to read every line of the file
I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago\n
I wrote the code below that successfully gets a random line from a file;
Im trying to draw a line. I wrote a code like below. UIColor *currentColor
Below is the text file, in which I want to read each line in
I wrote a very simple mysql line for my very simple search bar that
I'm trying out the Runtime.exec() method to run a command line process. I wrote
I wrote the beautiful python example code below. Now how do I make it
below is the stored proc I wrote: create or replace procedure test005 as begin
I wrote the below code in order to explain my issue. If I comment

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.