'None' Attribute while parsing with Beautiful Soup











up vote
2
down vote

favorite












I'm beginning with Python and BeautifulSoup. I want to scrape a website with BS and I don't understand my code result and the use of find and find_all. I want to get an URL in an href tag.



<div class="xBRiJc">
<a href="https://play.google.com/store/apps/collection/cluster?
clp=igNLChkKEzc4NDcxODQ2MTE5MjkxMDc4NTgQCBgDEiwKJmFhZGVtby5zdXBlcmF3ZXNvbWUudHYuYXdlc29tZWFkc2RlbW8yEAEYAxgB:S:ANO1ljKZ36s&amp;gsr=Ck6KA0sKGQoTNzg0NzE4NDYxMTkyOTEwNzg1OBAIGAMSLAomYWFkZW1vLnN1cGVyYXdlc29tZS50di5hd2Vzb21lYWRzZGVtbzIQARgDGAE%3D:S:ANO1ljKKOPI"> .
<h2 class="C7Bf8e bs3Xnd">SuperAwesome LTD</h2></a></div>


Here is my python code :



    developer_link = bs.find("div",{"class":"xBRiJc"})
print(developer_link.get('href'))


Why the result of my print command 'None' and not the URL in the href tag?










share|improve this question




















  • 7




    the href attribute is in the a tag inside the div tag... not on the div tag itself...
    – Jon Clements
    Nov 19 at 19:54










  • More over these are dynamic contents generated on the fly. class='xBRiJc' will not match in the next run. Look into a module that supports rendering dynamic contents like selenium or requests-html.
    – Idlehands
    Nov 19 at 20:11












  • @JonClements Thank you for your answer
    – userHG
    Nov 19 at 20:13















up vote
2
down vote

favorite












I'm beginning with Python and BeautifulSoup. I want to scrape a website with BS and I don't understand my code result and the use of find and find_all. I want to get an URL in an href tag.



<div class="xBRiJc">
<a href="https://play.google.com/store/apps/collection/cluster?
clp=igNLChkKEzc4NDcxODQ2MTE5MjkxMDc4NTgQCBgDEiwKJmFhZGVtby5zdXBlcmF3ZXNvbWUudHYuYXdlc29tZWFkc2RlbW8yEAEYAxgB:S:ANO1ljKZ36s&amp;gsr=Ck6KA0sKGQoTNzg0NzE4NDYxMTkyOTEwNzg1OBAIGAMSLAomYWFkZW1vLnN1cGVyYXdlc29tZS50di5hd2Vzb21lYWRzZGVtbzIQARgDGAE%3D:S:ANO1ljKKOPI"> .
<h2 class="C7Bf8e bs3Xnd">SuperAwesome LTD</h2></a></div>


Here is my python code :



    developer_link = bs.find("div",{"class":"xBRiJc"})
print(developer_link.get('href'))


Why the result of my print command 'None' and not the URL in the href tag?










share|improve this question




















  • 7




    the href attribute is in the a tag inside the div tag... not on the div tag itself...
    – Jon Clements
    Nov 19 at 19:54










  • More over these are dynamic contents generated on the fly. class='xBRiJc' will not match in the next run. Look into a module that supports rendering dynamic contents like selenium or requests-html.
    – Idlehands
    Nov 19 at 20:11












  • @JonClements Thank you for your answer
    – userHG
    Nov 19 at 20:13













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I'm beginning with Python and BeautifulSoup. I want to scrape a website with BS and I don't understand my code result and the use of find and find_all. I want to get an URL in an href tag.



<div class="xBRiJc">
<a href="https://play.google.com/store/apps/collection/cluster?
clp=igNLChkKEzc4NDcxODQ2MTE5MjkxMDc4NTgQCBgDEiwKJmFhZGVtby5zdXBlcmF3ZXNvbWUudHYuYXdlc29tZWFkc2RlbW8yEAEYAxgB:S:ANO1ljKZ36s&amp;gsr=Ck6KA0sKGQoTNzg0NzE4NDYxMTkyOTEwNzg1OBAIGAMSLAomYWFkZW1vLnN1cGVyYXdlc29tZS50di5hd2Vzb21lYWRzZGVtbzIQARgDGAE%3D:S:ANO1ljKKOPI"> .
<h2 class="C7Bf8e bs3Xnd">SuperAwesome LTD</h2></a></div>


Here is my python code :



    developer_link = bs.find("div",{"class":"xBRiJc"})
print(developer_link.get('href'))


Why the result of my print command 'None' and not the URL in the href tag?










share|improve this question















I'm beginning with Python and BeautifulSoup. I want to scrape a website with BS and I don't understand my code result and the use of find and find_all. I want to get an URL in an href tag.



<div class="xBRiJc">
<a href="https://play.google.com/store/apps/collection/cluster?
clp=igNLChkKEzc4NDcxODQ2MTE5MjkxMDc4NTgQCBgDEiwKJmFhZGVtby5zdXBlcmF3ZXNvbWUudHYuYXdlc29tZWFkc2RlbW8yEAEYAxgB:S:ANO1ljKZ36s&amp;gsr=Ck6KA0sKGQoTNzg0NzE4NDYxMTkyOTEwNzg1OBAIGAMSLAomYWFkZW1vLnN1cGVyYXdlc29tZS50di5hd2Vzb21lYWRzZGVtbzIQARgDGAE%3D:S:ANO1ljKKOPI"> .
<h2 class="C7Bf8e bs3Xnd">SuperAwesome LTD</h2></a></div>


Here is my python code :



    developer_link = bs.find("div",{"class":"xBRiJc"})
print(developer_link.get('href'))


Why the result of my print command 'None' and not the URL in the href tag?







python beautifulsoup






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 19:55









Reinderien

6,10122756




6,10122756










asked Nov 19 at 19:52









userHG

997




997








  • 7




    the href attribute is in the a tag inside the div tag... not on the div tag itself...
    – Jon Clements
    Nov 19 at 19:54










  • More over these are dynamic contents generated on the fly. class='xBRiJc' will not match in the next run. Look into a module that supports rendering dynamic contents like selenium or requests-html.
    – Idlehands
    Nov 19 at 20:11












  • @JonClements Thank you for your answer
    – userHG
    Nov 19 at 20:13














  • 7




    the href attribute is in the a tag inside the div tag... not on the div tag itself...
    – Jon Clements
    Nov 19 at 19:54










  • More over these are dynamic contents generated on the fly. class='xBRiJc' will not match in the next run. Look into a module that supports rendering dynamic contents like selenium or requests-html.
    – Idlehands
    Nov 19 at 20:11












  • @JonClements Thank you for your answer
    – userHG
    Nov 19 at 20:13








7




7




the href attribute is in the a tag inside the div tag... not on the div tag itself...
– Jon Clements
Nov 19 at 19:54




the href attribute is in the a tag inside the div tag... not on the div tag itself...
– Jon Clements
Nov 19 at 19:54












More over these are dynamic contents generated on the fly. class='xBRiJc' will not match in the next run. Look into a module that supports rendering dynamic contents like selenium or requests-html.
– Idlehands
Nov 19 at 20:11






More over these are dynamic contents generated on the fly. class='xBRiJc' will not match in the next run. Look into a module that supports rendering dynamic contents like selenium or requests-html.
– Idlehands
Nov 19 at 20:11














@JonClements Thank you for your answer
– userHG
Nov 19 at 20:13




@JonClements Thank you for your answer
– userHG
Nov 19 at 20:13












1 Answer
1






active

oldest

votes

















up vote
1
down vote













You're defining developer_link to be the <div> tag containing the link, instead of the link itself. Since the div tag itself has no "href" parameter, developer_link.get('href') will return None. So you just have to take it a step further:



>>> pagecode = """
... <div class="xBRiJc">
... ... <a href="https://play.google.com/store/apps/collection/cluster?
... ... clp=igNLChkKEzc4NDcxODQ2MTE5MjkxMDc4NTgQCBgDEiwKJmFhZGVtby5zdXBlcmF3ZXNvbWUudHYuYXdlc29tZWFkc2RlbW8yEAEYAxgB:S:ANO1ljKZ36s&amp;gsr=Ck6KA0sKGQoTNzg0NzE4NDYxMTkyOTEwNzg1OBAIGAMSLAomYWFkZW1vLnN1cGVyYXdlc29tZS50di5hd2Vzb21lYWRzZGVtbzIQARgDGAE%3D:S:ANO1ljKKOPI"> .
... ... <h2 class="C7Bf8e bs3Xnd">SuperAwesome LTD</h2></a></div>
... ... """
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(pagecode, 'lxml')
>>> div = soup.find("div", class_="xBRiJc")
>>> link = div.find("a")
>>> print(link.get('href'))
https://play.google.com/store/apps/collection/cluster?
... clp=igNLChkKEzc4NDcxODQ2MTE5MjkxMDc4NTgQCBgDEiwKJmFhZGVtby5zdXBlcmF3ZXNvbWUudHYuYXdlc29tZWFkc2RlbW8yEAEYAxgB:S:ANO1ljKZ36s&gsr=Ck6KA0sKGQoTNzg0NzE4NDYxMTkyOTEwNzg1OBAIGAMSLAomYWFkZW1vLnN1cGVyYXdlc29tZS50di5hd2Vzb21lYWRzZGVtbzIQARgDGAE%3D:S:ANO1ljKKOPI


Looking at this example though, I'm guessing that the div's class is something that's dynamically generated. If so, then the div's class may not be "xBRiJc" when you revisit the page, which means it's not a reliable identifier of the link. If you're just trying to get the first link whose text contains "SuperAwesome LTD", you could use some regex tricks to get the link based on just tht. But if you know your link has an H2 tag directly inside whose actual text is "SuperAwesome LTD", then you could do this:



t = soup.find('h2')
print(t.parent.get('href'))





share|improve this answer

















  • 1




    Welcome, and thanks contributing this answer. FWIW that's probably a Google "xid" which is just a (proprietary) hash of an identifier like GooglePlaySomethingWidget. It should be as stable as any other class name you'd match on.
    – user
    Nov 19 at 21:17











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381699%2fnone-attribute-while-parsing-with-beautiful-soup%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













You're defining developer_link to be the <div> tag containing the link, instead of the link itself. Since the div tag itself has no "href" parameter, developer_link.get('href') will return None. So you just have to take it a step further:



>>> pagecode = """
... <div class="xBRiJc">
... ... <a href="https://play.google.com/store/apps/collection/cluster?
... ... clp=igNLChkKEzc4NDcxODQ2MTE5MjkxMDc4NTgQCBgDEiwKJmFhZGVtby5zdXBlcmF3ZXNvbWUudHYuYXdlc29tZWFkc2RlbW8yEAEYAxgB:S:ANO1ljKZ36s&amp;gsr=Ck6KA0sKGQoTNzg0NzE4NDYxMTkyOTEwNzg1OBAIGAMSLAomYWFkZW1vLnN1cGVyYXdlc29tZS50di5hd2Vzb21lYWRzZGVtbzIQARgDGAE%3D:S:ANO1ljKKOPI"> .
... ... <h2 class="C7Bf8e bs3Xnd">SuperAwesome LTD</h2></a></div>
... ... """
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(pagecode, 'lxml')
>>> div = soup.find("div", class_="xBRiJc")
>>> link = div.find("a")
>>> print(link.get('href'))
https://play.google.com/store/apps/collection/cluster?
... clp=igNLChkKEzc4NDcxODQ2MTE5MjkxMDc4NTgQCBgDEiwKJmFhZGVtby5zdXBlcmF3ZXNvbWUudHYuYXdlc29tZWFkc2RlbW8yEAEYAxgB:S:ANO1ljKZ36s&gsr=Ck6KA0sKGQoTNzg0NzE4NDYxMTkyOTEwNzg1OBAIGAMSLAomYWFkZW1vLnN1cGVyYXdlc29tZS50di5hd2Vzb21lYWRzZGVtbzIQARgDGAE%3D:S:ANO1ljKKOPI


Looking at this example though, I'm guessing that the div's class is something that's dynamically generated. If so, then the div's class may not be "xBRiJc" when you revisit the page, which means it's not a reliable identifier of the link. If you're just trying to get the first link whose text contains "SuperAwesome LTD", you could use some regex tricks to get the link based on just tht. But if you know your link has an H2 tag directly inside whose actual text is "SuperAwesome LTD", then you could do this:



t = soup.find('h2')
print(t.parent.get('href'))





share|improve this answer

















  • 1




    Welcome, and thanks contributing this answer. FWIW that's probably a Google "xid" which is just a (proprietary) hash of an identifier like GooglePlaySomethingWidget. It should be as stable as any other class name you'd match on.
    – user
    Nov 19 at 21:17















up vote
1
down vote













You're defining developer_link to be the <div> tag containing the link, instead of the link itself. Since the div tag itself has no "href" parameter, developer_link.get('href') will return None. So you just have to take it a step further:



>>> pagecode = """
... <div class="xBRiJc">
... ... <a href="https://play.google.com/store/apps/collection/cluster?
... ... clp=igNLChkKEzc4NDcxODQ2MTE5MjkxMDc4NTgQCBgDEiwKJmFhZGVtby5zdXBlcmF3ZXNvbWUudHYuYXdlc29tZWFkc2RlbW8yEAEYAxgB:S:ANO1ljKZ36s&amp;gsr=Ck6KA0sKGQoTNzg0NzE4NDYxMTkyOTEwNzg1OBAIGAMSLAomYWFkZW1vLnN1cGVyYXdlc29tZS50di5hd2Vzb21lYWRzZGVtbzIQARgDGAE%3D:S:ANO1ljKKOPI"> .
... ... <h2 class="C7Bf8e bs3Xnd">SuperAwesome LTD</h2></a></div>
... ... """
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(pagecode, 'lxml')
>>> div = soup.find("div", class_="xBRiJc")
>>> link = div.find("a")
>>> print(link.get('href'))
https://play.google.com/store/apps/collection/cluster?
... clp=igNLChkKEzc4NDcxODQ2MTE5MjkxMDc4NTgQCBgDEiwKJmFhZGVtby5zdXBlcmF3ZXNvbWUudHYuYXdlc29tZWFkc2RlbW8yEAEYAxgB:S:ANO1ljKZ36s&gsr=Ck6KA0sKGQoTNzg0NzE4NDYxMTkyOTEwNzg1OBAIGAMSLAomYWFkZW1vLnN1cGVyYXdlc29tZS50di5hd2Vzb21lYWRzZGVtbzIQARgDGAE%3D:S:ANO1ljKKOPI


Looking at this example though, I'm guessing that the div's class is something that's dynamically generated. If so, then the div's class may not be "xBRiJc" when you revisit the page, which means it's not a reliable identifier of the link. If you're just trying to get the first link whose text contains "SuperAwesome LTD", you could use some regex tricks to get the link based on just tht. But if you know your link has an H2 tag directly inside whose actual text is "SuperAwesome LTD", then you could do this:



t = soup.find('h2')
print(t.parent.get('href'))





share|improve this answer

















  • 1




    Welcome, and thanks contributing this answer. FWIW that's probably a Google "xid" which is just a (proprietary) hash of an identifier like GooglePlaySomethingWidget. It should be as stable as any other class name you'd match on.
    – user
    Nov 19 at 21:17













up vote
1
down vote










up vote
1
down vote









You're defining developer_link to be the <div> tag containing the link, instead of the link itself. Since the div tag itself has no "href" parameter, developer_link.get('href') will return None. So you just have to take it a step further:



>>> pagecode = """
... <div class="xBRiJc">
... ... <a href="https://play.google.com/store/apps/collection/cluster?
... ... clp=igNLChkKEzc4NDcxODQ2MTE5MjkxMDc4NTgQCBgDEiwKJmFhZGVtby5zdXBlcmF3ZXNvbWUudHYuYXdlc29tZWFkc2RlbW8yEAEYAxgB:S:ANO1ljKZ36s&amp;gsr=Ck6KA0sKGQoTNzg0NzE4NDYxMTkyOTEwNzg1OBAIGAMSLAomYWFkZW1vLnN1cGVyYXdlc29tZS50di5hd2Vzb21lYWRzZGVtbzIQARgDGAE%3D:S:ANO1ljKKOPI"> .
... ... <h2 class="C7Bf8e bs3Xnd">SuperAwesome LTD</h2></a></div>
... ... """
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(pagecode, 'lxml')
>>> div = soup.find("div", class_="xBRiJc")
>>> link = div.find("a")
>>> print(link.get('href'))
https://play.google.com/store/apps/collection/cluster?
... clp=igNLChkKEzc4NDcxODQ2MTE5MjkxMDc4NTgQCBgDEiwKJmFhZGVtby5zdXBlcmF3ZXNvbWUudHYuYXdlc29tZWFkc2RlbW8yEAEYAxgB:S:ANO1ljKZ36s&gsr=Ck6KA0sKGQoTNzg0NzE4NDYxMTkyOTEwNzg1OBAIGAMSLAomYWFkZW1vLnN1cGVyYXdlc29tZS50di5hd2Vzb21lYWRzZGVtbzIQARgDGAE%3D:S:ANO1ljKKOPI


Looking at this example though, I'm guessing that the div's class is something that's dynamically generated. If so, then the div's class may not be "xBRiJc" when you revisit the page, which means it's not a reliable identifier of the link. If you're just trying to get the first link whose text contains "SuperAwesome LTD", you could use some regex tricks to get the link based on just tht. But if you know your link has an H2 tag directly inside whose actual text is "SuperAwesome LTD", then you could do this:



t = soup.find('h2')
print(t.parent.get('href'))





share|improve this answer












You're defining developer_link to be the <div> tag containing the link, instead of the link itself. Since the div tag itself has no "href" parameter, developer_link.get('href') will return None. So you just have to take it a step further:



>>> pagecode = """
... <div class="xBRiJc">
... ... <a href="https://play.google.com/store/apps/collection/cluster?
... ... clp=igNLChkKEzc4NDcxODQ2MTE5MjkxMDc4NTgQCBgDEiwKJmFhZGVtby5zdXBlcmF3ZXNvbWUudHYuYXdlc29tZWFkc2RlbW8yEAEYAxgB:S:ANO1ljKZ36s&amp;gsr=Ck6KA0sKGQoTNzg0NzE4NDYxMTkyOTEwNzg1OBAIGAMSLAomYWFkZW1vLnN1cGVyYXdlc29tZS50di5hd2Vzb21lYWRzZGVtbzIQARgDGAE%3D:S:ANO1ljKKOPI"> .
... ... <h2 class="C7Bf8e bs3Xnd">SuperAwesome LTD</h2></a></div>
... ... """
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(pagecode, 'lxml')
>>> div = soup.find("div", class_="xBRiJc")
>>> link = div.find("a")
>>> print(link.get('href'))
https://play.google.com/store/apps/collection/cluster?
... clp=igNLChkKEzc4NDcxODQ2MTE5MjkxMDc4NTgQCBgDEiwKJmFhZGVtby5zdXBlcmF3ZXNvbWUudHYuYXdlc29tZWFkc2RlbW8yEAEYAxgB:S:ANO1ljKZ36s&gsr=Ck6KA0sKGQoTNzg0NzE4NDYxMTkyOTEwNzg1OBAIGAMSLAomYWFkZW1vLnN1cGVyYXdlc29tZS50di5hd2Vzb21lYWRzZGVtbzIQARgDGAE%3D:S:ANO1ljKKOPI


Looking at this example though, I'm guessing that the div's class is something that's dynamically generated. If so, then the div's class may not be "xBRiJc" when you revisit the page, which means it's not a reliable identifier of the link. If you're just trying to get the first link whose text contains "SuperAwesome LTD", you could use some regex tricks to get the link based on just tht. But if you know your link has an H2 tag directly inside whose actual text is "SuperAwesome LTD", then you could do this:



t = soup.find('h2')
print(t.parent.get('href'))






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 at 21:13









Bill M.

3928




3928








  • 1




    Welcome, and thanks contributing this answer. FWIW that's probably a Google "xid" which is just a (proprietary) hash of an identifier like GooglePlaySomethingWidget. It should be as stable as any other class name you'd match on.
    – user
    Nov 19 at 21:17














  • 1




    Welcome, and thanks contributing this answer. FWIW that's probably a Google "xid" which is just a (proprietary) hash of an identifier like GooglePlaySomethingWidget. It should be as stable as any other class name you'd match on.
    – user
    Nov 19 at 21:17








1




1




Welcome, and thanks contributing this answer. FWIW that's probably a Google "xid" which is just a (proprietary) hash of an identifier like GooglePlaySomethingWidget. It should be as stable as any other class name you'd match on.
– user
Nov 19 at 21:17




Welcome, and thanks contributing this answer. FWIW that's probably a Google "xid" which is just a (proprietary) hash of an identifier like GooglePlaySomethingWidget. It should be as stable as any other class name you'd match on.
– user
Nov 19 at 21:17


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381699%2fnone-attribute-while-parsing-with-beautiful-soup%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?