473,406 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

B-Soup: broken iterator, tag a keyword?

Hi there,
I have the following using Beautiful Soup:

soup = BeautifulSoup(data)
tags = soup.findAll(href=re.compile("/MER_FRS_L2_Canada/MER_FRS_\S
+gz"))
for tag in tags:
print tag['href']
print tag.parent.nextSibling.string
print tag.parent.nextSibling.nextSibling.string
print tag.parent.nextSibling.nextSibling.nextSibling.str ing
print
tag.parent.nextSibling.nextSibling.nextSibling.nex tSibling.contents[0].string
For some reason I do not understand, using 'tag' as an iterator breaks
the code. Can someone tell me why? reading dir(soup) did not
illuminate me.

Thanks
Jul 10 '08 #1
1 1056
Hi,

Brendan wrote:
I have the following using Beautiful Soup:

soup = BeautifulSoup(data)
tags = soup.findAll(href=re.compile("/MER_FRS_L2_Canada/MER_FRS_\S
+gz"))
for tag in tags:
print tag['href']
print tag.parent.nextSibling.string
print tag.parent.nextSibling.nextSibling.string
print tag.parent.nextSibling.nextSibling.nextSibling.str ing
print
tag.parent.nextSibling.nextSibling.nextSibling.nex tSibling.contents[0].string
It's pretty impossible that the problem is the name "tag" here. But since you
didn't state what the actual problem is, let me suggest not to parse markup
with regular expressions in general (which BS does). Use a real XML/HTML
parser for that. lxml will work just fine (and it also has a nicer API).

http://codespeak.net/lxml/

Stefan
Jul 11 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Anonymous | last post by:
I'm trying to port code from VC.net to VC.net 2003. Here's a typical piece of code that's often used: template<class T> class PseudoContainer { typedef T::iterator iterator; // this line will...
4
by: John | last post by:
I have a function declaration that gives an error while compiling. Can anyone help me figure this one out? inline void create(const std::vector< myclass >& plist, std::vector< myclass...
54
by: Sahil Malik [MVP] | last post by:
What the heck - I can't find it. A bit shocked to see it missing though. So "Does VB.NET have the yield keyword, or any equivalent of it" ? -- - Sahil Malik Upcoming ADO.NET 2.0 book -...
2
by: Lorenzo Castelli | last post by:
This is an old problem of mine. Basically I have an abstract base class which represents a generic iterator over a collection of elements, and various derived classes that implement the...
5
by: Bill Oliver | last post by:
Help! I am writing an image processing package. For one constructor, I allow creating an image from a map of points and color values. The points are of a "position" class and the colors are...
1
by: atomik.fungus | last post by:
Hi, I'm re-writting my matrix class to practice my programming and the computer doesn't let me compile the next code: ( this example come from the constructor of the class) //the matrix is made...
10
by: desktop | last post by:
I have read about input, output forward etc iterators. But if I make the following: int myints = {1,2,3,4,5,1,2,3,4,5}; std::vector<intmyvector (myints,myints+10); std::vector<int>::iterator...
1
by: David Bilsby | last post by:
All Apologies for cross posing this but I am not sure if this is a VC 8 STL bug or simply an invalid use of the iterator. I have a PCI card access class which basically abstracts a third party...
8
by: Steven D'Aprano | last post by:
According to the Python docs, once an iterator raises StopIteration, it should continue to raise StopIteration forever. Iterators that fail to behave in this fashion are deemed to be "broken": ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.