Using Beautiful Soup to count links on requested page
This should be fairly straightforward. I want to count the links created from a search on a webpage. In this example, search for "gwen stefani" on Stack Overflow. As of the time of writing, the number of results is 15.
import bs4 # beautiful soup 4
import requests
import webbrowser
url = "https://stackoverflow.com/search?q=gwen+stefani"
myURL = url
webbrowser.open(myURL)
page = requests.get(url).text
r = requests.get(myURL)
html_content = r.text
soup = bs4.BeautifulSoup(html_content, "html.parser")
print soup.title
for link in soup.find_all("a"):
print(link.get("href"))
When the links are printed out, it doesn't contain any of the results mentioned. I'm new to the soup, and I'm not sure where I'm going wrong at this point.
python-2.7 web-scraping beautifulsoup
add a comment |
This should be fairly straightforward. I want to count the links created from a search on a webpage. In this example, search for "gwen stefani" on Stack Overflow. As of the time of writing, the number of results is 15.
import bs4 # beautiful soup 4
import requests
import webbrowser
url = "https://stackoverflow.com/search?q=gwen+stefani"
myURL = url
webbrowser.open(myURL)
page = requests.get(url).text
r = requests.get(myURL)
html_content = r.text
soup = bs4.BeautifulSoup(html_content, "html.parser")
print soup.title
for link in soup.find_all("a"):
print(link.get("href"))
When the links are printed out, it doesn't contain any of the results mentioned. I'm new to the soup, and I'm not sure where I'm going wrong at this point.
python-2.7 web-scraping beautifulsoup
You don't need webbrowser, just BeautifulSoup and requests.
– Kamikaze_goldfish
Nov 12 at 15:18
I'm using webbrowser to double-check my results :)
– Ghoul Fool
Nov 12 at 15:18
add a comment |
This should be fairly straightforward. I want to count the links created from a search on a webpage. In this example, search for "gwen stefani" on Stack Overflow. As of the time of writing, the number of results is 15.
import bs4 # beautiful soup 4
import requests
import webbrowser
url = "https://stackoverflow.com/search?q=gwen+stefani"
myURL = url
webbrowser.open(myURL)
page = requests.get(url).text
r = requests.get(myURL)
html_content = r.text
soup = bs4.BeautifulSoup(html_content, "html.parser")
print soup.title
for link in soup.find_all("a"):
print(link.get("href"))
When the links are printed out, it doesn't contain any of the results mentioned. I'm new to the soup, and I'm not sure where I'm going wrong at this point.
python-2.7 web-scraping beautifulsoup
This should be fairly straightforward. I want to count the links created from a search on a webpage. In this example, search for "gwen stefani" on Stack Overflow. As of the time of writing, the number of results is 15.
import bs4 # beautiful soup 4
import requests
import webbrowser
url = "https://stackoverflow.com/search?q=gwen+stefani"
myURL = url
webbrowser.open(myURL)
page = requests.get(url).text
r = requests.get(myURL)
html_content = r.text
soup = bs4.BeautifulSoup(html_content, "html.parser")
print soup.title
for link in soup.find_all("a"):
print(link.get("href"))
When the links are printed out, it doesn't contain any of the results mentioned. I'm new to the soup, and I'm not sure where I'm going wrong at this point.
python-2.7 web-scraping beautifulsoup
python-2.7 web-scraping beautifulsoup
asked Nov 12 at 14:41
Ghoul Fool
2,29373475
2,29373475
You don't need webbrowser, just BeautifulSoup and requests.
– Kamikaze_goldfish
Nov 12 at 15:18
I'm using webbrowser to double-check my results :)
– Ghoul Fool
Nov 12 at 15:18
add a comment |
You don't need webbrowser, just BeautifulSoup and requests.
– Kamikaze_goldfish
Nov 12 at 15:18
I'm using webbrowser to double-check my results :)
– Ghoul Fool
Nov 12 at 15:18
You don't need webbrowser, just BeautifulSoup and requests.
– Kamikaze_goldfish
Nov 12 at 15:18
You don't need webbrowser, just BeautifulSoup and requests.
– Kamikaze_goldfish
Nov 12 at 15:18
I'm using webbrowser to double-check my results :)
– Ghoul Fool
Nov 12 at 15:18
I'm using webbrowser to double-check my results :)
– Ghoul Fool
Nov 12 at 15:18
add a comment |
2 Answers
2
active
oldest
votes
I'm using python 3.x so you might have to adjust for that but I am getting all 15 links.
from bs4 import BeautifulSoup
import requests
url = 'https://stackoverflow.com/search?q=gwen+stefani'
page = requests.get(url)
soup = BeautifulSoup(page.text, 'hmtl.parser')
for link in soup.findAll('div', class_='result-link'):
print('https://stackoverflow.com'+link.a['href'])
sadly that results in an errorTraceback (most recent call last): File "D:LearnPythonget_gwen.py", line 6, in <module> soup = BeautifulSoup(page.text, 'lxml') File "C:Python27libsite-packagesbs4__init__.py", line 156, in __init__ % ",".join(features)) bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?
– Ghoul Fool
Nov 12 at 15:19
change the'lxml'
to'html.parser'
in thesoup
variable
– Kamikaze_goldfish
Nov 12 at 15:21
That's fixed it! Thank you.
– Ghoul Fool
Nov 12 at 15:25
You're welcome.. Glad I could help.
– Kamikaze_goldfish
Nov 12 at 15:30
add a comment |
You can also try below code where you do not need to use the class of div
element.
Just inspect the page and find the class of question's link.
import bs4 # beautiful soup 4
import requests
import webbrowser
import json
url = "https://stackoverflow.com/search?q=gwen+stefani"
webbrowser.open(url)
r = requests.get(url)
html_content = r.text
# with open('response.html', 'w', encoding="utf-8") as f:
# f.write(html_content)
soup = bs4.BeautifulSoup(html_content, "html.parser")
print(soup.title)
links = soup.find_all("a", class_='question-hyperlink')
valid_links =
for i, link in enumerate(links):
href = link.get('href').strip()
if href.startswith('/questions/'):
valid_links[href] = link.text.strip()
print(json.dumps(valid_links, indent=4)) # pretty printing dictionary
print(len(valid_links)) # 15
Output
<title>Posts containing 'gwen stefani' - Stack Overflow</title>
"/questions/39268369/what-does-minus-minus-do-in-excel": "Q: What does u2014 (minus minus) do in Excel? [duplicate]",
"/questions/53264513/using-beautiful-soup-to-count-links-on-requested-page": "Q: Using Beautiful Soup to count links on requested page",
"/questions/31074289/is-there-a-script-that-can-transfer-text-from-an-excel-file-into-an-adobe-design/31100563#31100563": "A: Is there a script that can transfer text from an excel file into an Adobe design program?",
"/questions/39268369/what-does-minus-minus-do-in-excel/39268800#39268800": "A: What does u2014 (minus minus) do in Excel?",
"/questions/1668447/launch-failed-binary-not-found-snow-leopard-and-eclipse-c-c-ide-issue/8463357#8463357": "A: u201cLaunch Failed. Binary Not Found.u201d Snow Leopard and Eclipse C/C++ IDE issue",
"/questions/33023818/split-and-rejoin-path-without-trailing-backslash": "Q: Split and rejoin path without trailing backslash",
"/questions/36986461/regex-match-return-remaining-rest-of-string": "Q: Regex match, return remaining rest of string",
"/questions/44686123/pass-variable-from-javascript-to-windows-batch-file": "Q: Pass variable from JavaScript to Windows batch file",
"/questions/44686123/pass-variable-from-javascript-to-windows-batch-file/44686309#44686309": "A: Pass variable from JavaScript to Windows batch file",
"/questions/52465425/reversing-a-list-with-single-element-gives-none": "Q: Reversing a list with single element gives None [duplicate]",
"/questions/22196612/array-length-outside-of-a-method": "Q: Array length outside of a method",
"/questions/13300815/not-getting-expected-results-from-select-query/13300920#13300920": "A: Not getting expected results from SELECT query",
"/questions/32884087/slicing-string-from-start": "Q: Slicing string from start [duplicate]",
"/questions/53264513/using-beautiful-soup-to-count-links-on-requested-page/53265048#53265048": "A: Using Beautiful Soup to count links on requested page",
"/questions/23337218/recursive-conditions-missing-base-case": "Q: Recursive conditions - missing base case"
15
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264513%2fusing-beautiful-soup-to-count-links-on-requested-page%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I'm using python 3.x so you might have to adjust for that but I am getting all 15 links.
from bs4 import BeautifulSoup
import requests
url = 'https://stackoverflow.com/search?q=gwen+stefani'
page = requests.get(url)
soup = BeautifulSoup(page.text, 'hmtl.parser')
for link in soup.findAll('div', class_='result-link'):
print('https://stackoverflow.com'+link.a['href'])
sadly that results in an errorTraceback (most recent call last): File "D:LearnPythonget_gwen.py", line 6, in <module> soup = BeautifulSoup(page.text, 'lxml') File "C:Python27libsite-packagesbs4__init__.py", line 156, in __init__ % ",".join(features)) bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?
– Ghoul Fool
Nov 12 at 15:19
change the'lxml'
to'html.parser'
in thesoup
variable
– Kamikaze_goldfish
Nov 12 at 15:21
That's fixed it! Thank you.
– Ghoul Fool
Nov 12 at 15:25
You're welcome.. Glad I could help.
– Kamikaze_goldfish
Nov 12 at 15:30
add a comment |
I'm using python 3.x so you might have to adjust for that but I am getting all 15 links.
from bs4 import BeautifulSoup
import requests
url = 'https://stackoverflow.com/search?q=gwen+stefani'
page = requests.get(url)
soup = BeautifulSoup(page.text, 'hmtl.parser')
for link in soup.findAll('div', class_='result-link'):
print('https://stackoverflow.com'+link.a['href'])
sadly that results in an errorTraceback (most recent call last): File "D:LearnPythonget_gwen.py", line 6, in <module> soup = BeautifulSoup(page.text, 'lxml') File "C:Python27libsite-packagesbs4__init__.py", line 156, in __init__ % ",".join(features)) bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?
– Ghoul Fool
Nov 12 at 15:19
change the'lxml'
to'html.parser'
in thesoup
variable
– Kamikaze_goldfish
Nov 12 at 15:21
That's fixed it! Thank you.
– Ghoul Fool
Nov 12 at 15:25
You're welcome.. Glad I could help.
– Kamikaze_goldfish
Nov 12 at 15:30
add a comment |
I'm using python 3.x so you might have to adjust for that but I am getting all 15 links.
from bs4 import BeautifulSoup
import requests
url = 'https://stackoverflow.com/search?q=gwen+stefani'
page = requests.get(url)
soup = BeautifulSoup(page.text, 'hmtl.parser')
for link in soup.findAll('div', class_='result-link'):
print('https://stackoverflow.com'+link.a['href'])
I'm using python 3.x so you might have to adjust for that but I am getting all 15 links.
from bs4 import BeautifulSoup
import requests
url = 'https://stackoverflow.com/search?q=gwen+stefani'
page = requests.get(url)
soup = BeautifulSoup(page.text, 'hmtl.parser')
for link in soup.findAll('div', class_='result-link'):
print('https://stackoverflow.com'+link.a['href'])
edited Nov 12 at 15:20
answered Nov 12 at 15:13
Kamikaze_goldfish
463311
463311
sadly that results in an errorTraceback (most recent call last): File "D:LearnPythonget_gwen.py", line 6, in <module> soup = BeautifulSoup(page.text, 'lxml') File "C:Python27libsite-packagesbs4__init__.py", line 156, in __init__ % ",".join(features)) bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?
– Ghoul Fool
Nov 12 at 15:19
change the'lxml'
to'html.parser'
in thesoup
variable
– Kamikaze_goldfish
Nov 12 at 15:21
That's fixed it! Thank you.
– Ghoul Fool
Nov 12 at 15:25
You're welcome.. Glad I could help.
– Kamikaze_goldfish
Nov 12 at 15:30
add a comment |
sadly that results in an errorTraceback (most recent call last): File "D:LearnPythonget_gwen.py", line 6, in <module> soup = BeautifulSoup(page.text, 'lxml') File "C:Python27libsite-packagesbs4__init__.py", line 156, in __init__ % ",".join(features)) bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?
– Ghoul Fool
Nov 12 at 15:19
change the'lxml'
to'html.parser'
in thesoup
variable
– Kamikaze_goldfish
Nov 12 at 15:21
That's fixed it! Thank you.
– Ghoul Fool
Nov 12 at 15:25
You're welcome.. Glad I could help.
– Kamikaze_goldfish
Nov 12 at 15:30
sadly that results in an error
Traceback (most recent call last): File "D:LearnPythonget_gwen.py", line 6, in <module> soup = BeautifulSoup(page.text, 'lxml') File "C:Python27libsite-packagesbs4__init__.py", line 156, in __init__ % ",".join(features)) bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?
– Ghoul Fool
Nov 12 at 15:19
sadly that results in an error
Traceback (most recent call last): File "D:LearnPythonget_gwen.py", line 6, in <module> soup = BeautifulSoup(page.text, 'lxml') File "C:Python27libsite-packagesbs4__init__.py", line 156, in __init__ % ",".join(features)) bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?
– Ghoul Fool
Nov 12 at 15:19
change the
'lxml'
to 'html.parser'
in the soup
variable– Kamikaze_goldfish
Nov 12 at 15:21
change the
'lxml'
to 'html.parser'
in the soup
variable– Kamikaze_goldfish
Nov 12 at 15:21
That's fixed it! Thank you.
– Ghoul Fool
Nov 12 at 15:25
That's fixed it! Thank you.
– Ghoul Fool
Nov 12 at 15:25
You're welcome.. Glad I could help.
– Kamikaze_goldfish
Nov 12 at 15:30
You're welcome.. Glad I could help.
– Kamikaze_goldfish
Nov 12 at 15:30
add a comment |
You can also try below code where you do not need to use the class of div
element.
Just inspect the page and find the class of question's link.
import bs4 # beautiful soup 4
import requests
import webbrowser
import json
url = "https://stackoverflow.com/search?q=gwen+stefani"
webbrowser.open(url)
r = requests.get(url)
html_content = r.text
# with open('response.html', 'w', encoding="utf-8") as f:
# f.write(html_content)
soup = bs4.BeautifulSoup(html_content, "html.parser")
print(soup.title)
links = soup.find_all("a", class_='question-hyperlink')
valid_links =
for i, link in enumerate(links):
href = link.get('href').strip()
if href.startswith('/questions/'):
valid_links[href] = link.text.strip()
print(json.dumps(valid_links, indent=4)) # pretty printing dictionary
print(len(valid_links)) # 15
Output
<title>Posts containing 'gwen stefani' - Stack Overflow</title>
"/questions/39268369/what-does-minus-minus-do-in-excel": "Q: What does u2014 (minus minus) do in Excel? [duplicate]",
"/questions/53264513/using-beautiful-soup-to-count-links-on-requested-page": "Q: Using Beautiful Soup to count links on requested page",
"/questions/31074289/is-there-a-script-that-can-transfer-text-from-an-excel-file-into-an-adobe-design/31100563#31100563": "A: Is there a script that can transfer text from an excel file into an Adobe design program?",
"/questions/39268369/what-does-minus-minus-do-in-excel/39268800#39268800": "A: What does u2014 (minus minus) do in Excel?",
"/questions/1668447/launch-failed-binary-not-found-snow-leopard-and-eclipse-c-c-ide-issue/8463357#8463357": "A: u201cLaunch Failed. Binary Not Found.u201d Snow Leopard and Eclipse C/C++ IDE issue",
"/questions/33023818/split-and-rejoin-path-without-trailing-backslash": "Q: Split and rejoin path without trailing backslash",
"/questions/36986461/regex-match-return-remaining-rest-of-string": "Q: Regex match, return remaining rest of string",
"/questions/44686123/pass-variable-from-javascript-to-windows-batch-file": "Q: Pass variable from JavaScript to Windows batch file",
"/questions/44686123/pass-variable-from-javascript-to-windows-batch-file/44686309#44686309": "A: Pass variable from JavaScript to Windows batch file",
"/questions/52465425/reversing-a-list-with-single-element-gives-none": "Q: Reversing a list with single element gives None [duplicate]",
"/questions/22196612/array-length-outside-of-a-method": "Q: Array length outside of a method",
"/questions/13300815/not-getting-expected-results-from-select-query/13300920#13300920": "A: Not getting expected results from SELECT query",
"/questions/32884087/slicing-string-from-start": "Q: Slicing string from start [duplicate]",
"/questions/53264513/using-beautiful-soup-to-count-links-on-requested-page/53265048#53265048": "A: Using Beautiful Soup to count links on requested page",
"/questions/23337218/recursive-conditions-missing-base-case": "Q: Recursive conditions - missing base case"
15
add a comment |
You can also try below code where you do not need to use the class of div
element.
Just inspect the page and find the class of question's link.
import bs4 # beautiful soup 4
import requests
import webbrowser
import json
url = "https://stackoverflow.com/search?q=gwen+stefani"
webbrowser.open(url)
r = requests.get(url)
html_content = r.text
# with open('response.html', 'w', encoding="utf-8") as f:
# f.write(html_content)
soup = bs4.BeautifulSoup(html_content, "html.parser")
print(soup.title)
links = soup.find_all("a", class_='question-hyperlink')
valid_links =
for i, link in enumerate(links):
href = link.get('href').strip()
if href.startswith('/questions/'):
valid_links[href] = link.text.strip()
print(json.dumps(valid_links, indent=4)) # pretty printing dictionary
print(len(valid_links)) # 15
Output
<title>Posts containing 'gwen stefani' - Stack Overflow</title>
"/questions/39268369/what-does-minus-minus-do-in-excel": "Q: What does u2014 (minus minus) do in Excel? [duplicate]",
"/questions/53264513/using-beautiful-soup-to-count-links-on-requested-page": "Q: Using Beautiful Soup to count links on requested page",
"/questions/31074289/is-there-a-script-that-can-transfer-text-from-an-excel-file-into-an-adobe-design/31100563#31100563": "A: Is there a script that can transfer text from an excel file into an Adobe design program?",
"/questions/39268369/what-does-minus-minus-do-in-excel/39268800#39268800": "A: What does u2014 (minus minus) do in Excel?",
"/questions/1668447/launch-failed-binary-not-found-snow-leopard-and-eclipse-c-c-ide-issue/8463357#8463357": "A: u201cLaunch Failed. Binary Not Found.u201d Snow Leopard and Eclipse C/C++ IDE issue",
"/questions/33023818/split-and-rejoin-path-without-trailing-backslash": "Q: Split and rejoin path without trailing backslash",
"/questions/36986461/regex-match-return-remaining-rest-of-string": "Q: Regex match, return remaining rest of string",
"/questions/44686123/pass-variable-from-javascript-to-windows-batch-file": "Q: Pass variable from JavaScript to Windows batch file",
"/questions/44686123/pass-variable-from-javascript-to-windows-batch-file/44686309#44686309": "A: Pass variable from JavaScript to Windows batch file",
"/questions/52465425/reversing-a-list-with-single-element-gives-none": "Q: Reversing a list with single element gives None [duplicate]",
"/questions/22196612/array-length-outside-of-a-method": "Q: Array length outside of a method",
"/questions/13300815/not-getting-expected-results-from-select-query/13300920#13300920": "A: Not getting expected results from SELECT query",
"/questions/32884087/slicing-string-from-start": "Q: Slicing string from start [duplicate]",
"/questions/53264513/using-beautiful-soup-to-count-links-on-requested-page/53265048#53265048": "A: Using Beautiful Soup to count links on requested page",
"/questions/23337218/recursive-conditions-missing-base-case": "Q: Recursive conditions - missing base case"
15
add a comment |
You can also try below code where you do not need to use the class of div
element.
Just inspect the page and find the class of question's link.
import bs4 # beautiful soup 4
import requests
import webbrowser
import json
url = "https://stackoverflow.com/search?q=gwen+stefani"
webbrowser.open(url)
r = requests.get(url)
html_content = r.text
# with open('response.html', 'w', encoding="utf-8") as f:
# f.write(html_content)
soup = bs4.BeautifulSoup(html_content, "html.parser")
print(soup.title)
links = soup.find_all("a", class_='question-hyperlink')
valid_links =
for i, link in enumerate(links):
href = link.get('href').strip()
if href.startswith('/questions/'):
valid_links[href] = link.text.strip()
print(json.dumps(valid_links, indent=4)) # pretty printing dictionary
print(len(valid_links)) # 15
Output
<title>Posts containing 'gwen stefani' - Stack Overflow</title>
"/questions/39268369/what-does-minus-minus-do-in-excel": "Q: What does u2014 (minus minus) do in Excel? [duplicate]",
"/questions/53264513/using-beautiful-soup-to-count-links-on-requested-page": "Q: Using Beautiful Soup to count links on requested page",
"/questions/31074289/is-there-a-script-that-can-transfer-text-from-an-excel-file-into-an-adobe-design/31100563#31100563": "A: Is there a script that can transfer text from an excel file into an Adobe design program?",
"/questions/39268369/what-does-minus-minus-do-in-excel/39268800#39268800": "A: What does u2014 (minus minus) do in Excel?",
"/questions/1668447/launch-failed-binary-not-found-snow-leopard-and-eclipse-c-c-ide-issue/8463357#8463357": "A: u201cLaunch Failed. Binary Not Found.u201d Snow Leopard and Eclipse C/C++ IDE issue",
"/questions/33023818/split-and-rejoin-path-without-trailing-backslash": "Q: Split and rejoin path without trailing backslash",
"/questions/36986461/regex-match-return-remaining-rest-of-string": "Q: Regex match, return remaining rest of string",
"/questions/44686123/pass-variable-from-javascript-to-windows-batch-file": "Q: Pass variable from JavaScript to Windows batch file",
"/questions/44686123/pass-variable-from-javascript-to-windows-batch-file/44686309#44686309": "A: Pass variable from JavaScript to Windows batch file",
"/questions/52465425/reversing-a-list-with-single-element-gives-none": "Q: Reversing a list with single element gives None [duplicate]",
"/questions/22196612/array-length-outside-of-a-method": "Q: Array length outside of a method",
"/questions/13300815/not-getting-expected-results-from-select-query/13300920#13300920": "A: Not getting expected results from SELECT query",
"/questions/32884087/slicing-string-from-start": "Q: Slicing string from start [duplicate]",
"/questions/53264513/using-beautiful-soup-to-count-links-on-requested-page/53265048#53265048": "A: Using Beautiful Soup to count links on requested page",
"/questions/23337218/recursive-conditions-missing-base-case": "Q: Recursive conditions - missing base case"
15
You can also try below code where you do not need to use the class of div
element.
Just inspect the page and find the class of question's link.
import bs4 # beautiful soup 4
import requests
import webbrowser
import json
url = "https://stackoverflow.com/search?q=gwen+stefani"
webbrowser.open(url)
r = requests.get(url)
html_content = r.text
# with open('response.html', 'w', encoding="utf-8") as f:
# f.write(html_content)
soup = bs4.BeautifulSoup(html_content, "html.parser")
print(soup.title)
links = soup.find_all("a", class_='question-hyperlink')
valid_links =
for i, link in enumerate(links):
href = link.get('href').strip()
if href.startswith('/questions/'):
valid_links[href] = link.text.strip()
print(json.dumps(valid_links, indent=4)) # pretty printing dictionary
print(len(valid_links)) # 15
Output
<title>Posts containing 'gwen stefani' - Stack Overflow</title>
"/questions/39268369/what-does-minus-minus-do-in-excel": "Q: What does u2014 (minus minus) do in Excel? [duplicate]",
"/questions/53264513/using-beautiful-soup-to-count-links-on-requested-page": "Q: Using Beautiful Soup to count links on requested page",
"/questions/31074289/is-there-a-script-that-can-transfer-text-from-an-excel-file-into-an-adobe-design/31100563#31100563": "A: Is there a script that can transfer text from an excel file into an Adobe design program?",
"/questions/39268369/what-does-minus-minus-do-in-excel/39268800#39268800": "A: What does u2014 (minus minus) do in Excel?",
"/questions/1668447/launch-failed-binary-not-found-snow-leopard-and-eclipse-c-c-ide-issue/8463357#8463357": "A: u201cLaunch Failed. Binary Not Found.u201d Snow Leopard and Eclipse C/C++ IDE issue",
"/questions/33023818/split-and-rejoin-path-without-trailing-backslash": "Q: Split and rejoin path without trailing backslash",
"/questions/36986461/regex-match-return-remaining-rest-of-string": "Q: Regex match, return remaining rest of string",
"/questions/44686123/pass-variable-from-javascript-to-windows-batch-file": "Q: Pass variable from JavaScript to Windows batch file",
"/questions/44686123/pass-variable-from-javascript-to-windows-batch-file/44686309#44686309": "A: Pass variable from JavaScript to Windows batch file",
"/questions/52465425/reversing-a-list-with-single-element-gives-none": "Q: Reversing a list with single element gives None [duplicate]",
"/questions/22196612/array-length-outside-of-a-method": "Q: Array length outside of a method",
"/questions/13300815/not-getting-expected-results-from-select-query/13300920#13300920": "A: Not getting expected results from SELECT query",
"/questions/32884087/slicing-string-from-start": "Q: Slicing string from start [duplicate]",
"/questions/53264513/using-beautiful-soup-to-count-links-on-requested-page/53265048#53265048": "A: Using Beautiful Soup to count links on requested page",
"/questions/23337218/recursive-conditions-missing-base-case": "Q: Recursive conditions - missing base case"
15
edited Nov 12 at 15:57
answered Nov 12 at 15:44
hygull
3,25411228
3,25411228
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264513%2fusing-beautiful-soup-to-count-links-on-requested-page%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
You don't need webbrowser, just BeautifulSoup and requests.
– Kamikaze_goldfish
Nov 12 at 15:18
I'm using webbrowser to double-check my results :)
– Ghoul Fool
Nov 12 at 15:18