0

html:

 <li class="dropdown menu-large menu_index_link"><a href="/MainPage" title="A">A</a></li>
 <li class="dropdown menu-large menu_index_link"><a href="/apple" title="1">1</a></li>

They have the same html format but I only need the second one, what should I do with this? Maybe use title to distinguish?

Code:

for item in soup.find_all(attrs={'class':'dropdown menu-large menu_index_link'}):
    for link in item.find_all('a'):
        href=link.get('href')   #print out both of the link

Problem solved as below:

for item in soup.find_all(attrs={'class':'dropdown menu-large menu_index_link'}):
        for link in item.find_all('a', {'title': "1"}):
            href=link.get('href')   #print out the link I want
3
  • 1
    what about item.find_all('a', {'title': "1"}) ? Commented Oct 19, 2017 at 8:04
  • @anupsabraham works perfectly! thank you :) Commented Oct 19, 2017 at 8:08
  • Cool. Didn't that is the answer you were looking for. Will post it as an answer. Commented Oct 19, 2017 at 8:09

1 Answer 1

3

I see that the title attribute for both the a tags are different. You can select the required item by including title filter in your find_all.

item.find_all('a', {'title': "1"})
Sign up to request clarification or add additional context in comments.

8 Comments

One more question here (I hope it's okay!) If I have more than 5 titles in the same situation but this time I need three of them, Can I write them in one line too?
item.find_all('a', {'title' : '1', '2'}) I tried to do this but it's an error
Not quite sure what all items are you going to catch. But you can use regex to filter them. item.find_all('a', {'title': re.compile(r"\d")})
The above method should match all a tags with numerics in title.
More Specific, if my title is these choice: "A", "B", "C", "D", "E", "F" and I only want to extract from "B" and "E", I can use item.find_all('a', {'title': re.compile(r"\d")})? Thank you :D
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.