'str' object cannot be interpreted as an integer in python
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am scrapping cricket test match details i have tested the results now i want to save it inside the file. while saving the html in file I am getting str object cannot be interedpreted as an integer
this is my code
for i in range(0, 2000):
url = 'http://search.espncricinfo.com/ci/content/match/search.html?search=test;all=1;page=%s' %i
html = requests.get(url)
print ('Checking page %s of 2000' %(i+1))
soupy = bs4.BeautifulSoup(html.text, 'html.parser')
time.sleep(1)
for new_host in soupy.findAll('a', {'class' : 'srchPlyrNmTxt'}):
try:
new_host = new_host['href']
except:
continue
odiurl = BASE_URL + new_host
new_host = odiurl
print(new_host)
html = requests.get(new_host).text
with open('espncricinfo-fc/{0!s}'.format(str.split(new_host, "/")[4]), "wb") as f:
f.write(html)
I am getting this error str object cannot be interedpreted as an integer
I am getting error in this line
with open('espncricinfo-fc/{0!s}'.format(str.split(new_host, "/")[4]), "wb") as f:
python beautifulsoup
|
show 7 more comments
I am scrapping cricket test match details i have tested the results now i want to save it inside the file. while saving the html in file I am getting str object cannot be interedpreted as an integer
this is my code
for i in range(0, 2000):
url = 'http://search.espncricinfo.com/ci/content/match/search.html?search=test;all=1;page=%s' %i
html = requests.get(url)
print ('Checking page %s of 2000' %(i+1))
soupy = bs4.BeautifulSoup(html.text, 'html.parser')
time.sleep(1)
for new_host in soupy.findAll('a', {'class' : 'srchPlyrNmTxt'}):
try:
new_host = new_host['href']
except:
continue
odiurl = BASE_URL + new_host
new_host = odiurl
print(new_host)
html = requests.get(new_host).text
with open('espncricinfo-fc/{0!s}'.format(str.split(new_host, "/")[4]), "wb") as f:
f.write(html)
I am getting this error str object cannot be interedpreted as an integer
I am getting error in this line
with open('espncricinfo-fc/{0!s}'.format(str.split(new_host, "/")[4]), "wb") as f:
python beautifulsoup
Where are you getting the error? (which line?)
– Tomothy32
Nov 29 '18 at 5:25
last two lines i am getting error
– Tayyab Gulsher Vohra
Nov 29 '18 at 5:26
What is BASE_URL?
– Shivam Singh
Nov 29 '18 at 5:36
You're writing in byte mode ("wb"), but I'm guessing you're trying to writestrdata rather thanbytes. What happens if you changerequests.get(new_host).texttorequests.get(new_host).text.encode()?
– JaminSore
Nov 29 '18 at 5:39
@ShivamSingh BASE_URL = 'espncricinfo.com'
– Tayyab Gulsher Vohra
Nov 29 '18 at 5:39
|
show 7 more comments
I am scrapping cricket test match details i have tested the results now i want to save it inside the file. while saving the html in file I am getting str object cannot be interedpreted as an integer
this is my code
for i in range(0, 2000):
url = 'http://search.espncricinfo.com/ci/content/match/search.html?search=test;all=1;page=%s' %i
html = requests.get(url)
print ('Checking page %s of 2000' %(i+1))
soupy = bs4.BeautifulSoup(html.text, 'html.parser')
time.sleep(1)
for new_host in soupy.findAll('a', {'class' : 'srchPlyrNmTxt'}):
try:
new_host = new_host['href']
except:
continue
odiurl = BASE_URL + new_host
new_host = odiurl
print(new_host)
html = requests.get(new_host).text
with open('espncricinfo-fc/{0!s}'.format(str.split(new_host, "/")[4]), "wb") as f:
f.write(html)
I am getting this error str object cannot be interedpreted as an integer
I am getting error in this line
with open('espncricinfo-fc/{0!s}'.format(str.split(new_host, "/")[4]), "wb") as f:
python beautifulsoup
I am scrapping cricket test match details i have tested the results now i want to save it inside the file. while saving the html in file I am getting str object cannot be interedpreted as an integer
this is my code
for i in range(0, 2000):
url = 'http://search.espncricinfo.com/ci/content/match/search.html?search=test;all=1;page=%s' %i
html = requests.get(url)
print ('Checking page %s of 2000' %(i+1))
soupy = bs4.BeautifulSoup(html.text, 'html.parser')
time.sleep(1)
for new_host in soupy.findAll('a', {'class' : 'srchPlyrNmTxt'}):
try:
new_host = new_host['href']
except:
continue
odiurl = BASE_URL + new_host
new_host = odiurl
print(new_host)
html = requests.get(new_host).text
with open('espncricinfo-fc/{0!s}'.format(str.split(new_host, "/")[4]), "wb") as f:
f.write(html)
I am getting this error str object cannot be interedpreted as an integer
I am getting error in this line
with open('espncricinfo-fc/{0!s}'.format(str.split(new_host, "/")[4]), "wb") as f:
python beautifulsoup
python beautifulsoup
edited Nov 29 '18 at 5:51
Tayyab Gulsher Vohra
asked Nov 29 '18 at 5:24
Tayyab Gulsher VohraTayyab Gulsher Vohra
1681318
1681318
Where are you getting the error? (which line?)
– Tomothy32
Nov 29 '18 at 5:25
last two lines i am getting error
– Tayyab Gulsher Vohra
Nov 29 '18 at 5:26
What is BASE_URL?
– Shivam Singh
Nov 29 '18 at 5:36
You're writing in byte mode ("wb"), but I'm guessing you're trying to writestrdata rather thanbytes. What happens if you changerequests.get(new_host).texttorequests.get(new_host).text.encode()?
– JaminSore
Nov 29 '18 at 5:39
@ShivamSingh BASE_URL = 'espncricinfo.com'
– Tayyab Gulsher Vohra
Nov 29 '18 at 5:39
|
show 7 more comments
Where are you getting the error? (which line?)
– Tomothy32
Nov 29 '18 at 5:25
last two lines i am getting error
– Tayyab Gulsher Vohra
Nov 29 '18 at 5:26
What is BASE_URL?
– Shivam Singh
Nov 29 '18 at 5:36
You're writing in byte mode ("wb"), but I'm guessing you're trying to writestrdata rather thanbytes. What happens if you changerequests.get(new_host).texttorequests.get(new_host).text.encode()?
– JaminSore
Nov 29 '18 at 5:39
@ShivamSingh BASE_URL = 'espncricinfo.com'
– Tayyab Gulsher Vohra
Nov 29 '18 at 5:39
Where are you getting the error? (which line?)
– Tomothy32
Nov 29 '18 at 5:25
Where are you getting the error? (which line?)
– Tomothy32
Nov 29 '18 at 5:25
last two lines i am getting error
– Tayyab Gulsher Vohra
Nov 29 '18 at 5:26
last two lines i am getting error
– Tayyab Gulsher Vohra
Nov 29 '18 at 5:26
What is BASE_URL?
– Shivam Singh
Nov 29 '18 at 5:36
What is BASE_URL?
– Shivam Singh
Nov 29 '18 at 5:36
You're writing in byte mode (
"wb"), but I'm guessing you're trying to write str data rather than bytes. What happens if you change requests.get(new_host).text to requests.get(new_host).text.encode()?– JaminSore
Nov 29 '18 at 5:39
You're writing in byte mode (
"wb"), but I'm guessing you're trying to write str data rather than bytes. What happens if you change requests.get(new_host).text to requests.get(new_host).text.encode()?– JaminSore
Nov 29 '18 at 5:39
@ShivamSingh BASE_URL = 'espncricinfo.com'
– Tayyab Gulsher Vohra
Nov 29 '18 at 5:39
@ShivamSingh BASE_URL = 'espncricinfo.com'
– Tayyab Gulsher Vohra
Nov 29 '18 at 5:39
|
show 7 more comments
2 Answers
2
active
oldest
votes
If you are using Python 3.x, try changing the last line to
f.write(bytes(html, 'UTF-8'))
Also try this,
new_host = str(new_host['href'])
I am getting the same error as before 'str' object cannot be interpreted as an integer
– Tayyab Gulsher Vohra
Nov 29 '18 at 5:47
Updated the answer
– Shivam Singh
Nov 29 '18 at 5:56
Shivam the error has been eliminated but nothing is saving the file...
– Tayyab Gulsher Vohra
Nov 29 '18 at 6:00
You need to fix the split function, It should bestr.split(new_host, "/")[6]andBASE_URL = "http://espncricinfo.com"
– Shivam Singh
Nov 29 '18 at 6:01
Shivam done both of these above edit but still it is not saving in the file.
– Tayyab Gulsher Vohra
Nov 29 '18 at 6:05
|
show 2 more comments
The problem is your print statement.
It should read
print('checking %d etc.' % (i + 1))
its working i have checked i have also removed this line but the error remains same..
– Tayyab Gulsher Vohra
Nov 29 '18 at 5:41
What line number, doesnew_hostas a class have a__str__method. Youprintit and usestr.splitin the final line
– rhubarbdog
Nov 29 '18 at 5:47
its a variable..
– Tayyab Gulsher Vohra
Nov 29 '18 at 5:50
What doessoupy.findall('a', { ... })return. A range or a list of strings. You setnew_hosttoBASE_URL + new_host. After setting it to a dictionary lookupnew_host['href']
– rhubarbdog
Nov 29 '18 at 5:58
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%2f53532368%2fstr-object-cannot-be-interpreted-as-an-integer-in-python%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
If you are using Python 3.x, try changing the last line to
f.write(bytes(html, 'UTF-8'))
Also try this,
new_host = str(new_host['href'])
I am getting the same error as before 'str' object cannot be interpreted as an integer
– Tayyab Gulsher Vohra
Nov 29 '18 at 5:47
Updated the answer
– Shivam Singh
Nov 29 '18 at 5:56
Shivam the error has been eliminated but nothing is saving the file...
– Tayyab Gulsher Vohra
Nov 29 '18 at 6:00
You need to fix the split function, It should bestr.split(new_host, "/")[6]andBASE_URL = "http://espncricinfo.com"
– Shivam Singh
Nov 29 '18 at 6:01
Shivam done both of these above edit but still it is not saving in the file.
– Tayyab Gulsher Vohra
Nov 29 '18 at 6:05
|
show 2 more comments
If you are using Python 3.x, try changing the last line to
f.write(bytes(html, 'UTF-8'))
Also try this,
new_host = str(new_host['href'])
I am getting the same error as before 'str' object cannot be interpreted as an integer
– Tayyab Gulsher Vohra
Nov 29 '18 at 5:47
Updated the answer
– Shivam Singh
Nov 29 '18 at 5:56
Shivam the error has been eliminated but nothing is saving the file...
– Tayyab Gulsher Vohra
Nov 29 '18 at 6:00
You need to fix the split function, It should bestr.split(new_host, "/")[6]andBASE_URL = "http://espncricinfo.com"
– Shivam Singh
Nov 29 '18 at 6:01
Shivam done both of these above edit but still it is not saving in the file.
– Tayyab Gulsher Vohra
Nov 29 '18 at 6:05
|
show 2 more comments
If you are using Python 3.x, try changing the last line to
f.write(bytes(html, 'UTF-8'))
Also try this,
new_host = str(new_host['href'])
If you are using Python 3.x, try changing the last line to
f.write(bytes(html, 'UTF-8'))
Also try this,
new_host = str(new_host['href'])
edited Nov 29 '18 at 5:56
answered Nov 29 '18 at 5:43
Shivam SinghShivam Singh
1,271158
1,271158
I am getting the same error as before 'str' object cannot be interpreted as an integer
– Tayyab Gulsher Vohra
Nov 29 '18 at 5:47
Updated the answer
– Shivam Singh
Nov 29 '18 at 5:56
Shivam the error has been eliminated but nothing is saving the file...
– Tayyab Gulsher Vohra
Nov 29 '18 at 6:00
You need to fix the split function, It should bestr.split(new_host, "/")[6]andBASE_URL = "http://espncricinfo.com"
– Shivam Singh
Nov 29 '18 at 6:01
Shivam done both of these above edit but still it is not saving in the file.
– Tayyab Gulsher Vohra
Nov 29 '18 at 6:05
|
show 2 more comments
I am getting the same error as before 'str' object cannot be interpreted as an integer
– Tayyab Gulsher Vohra
Nov 29 '18 at 5:47
Updated the answer
– Shivam Singh
Nov 29 '18 at 5:56
Shivam the error has been eliminated but nothing is saving the file...
– Tayyab Gulsher Vohra
Nov 29 '18 at 6:00
You need to fix the split function, It should bestr.split(new_host, "/")[6]andBASE_URL = "http://espncricinfo.com"
– Shivam Singh
Nov 29 '18 at 6:01
Shivam done both of these above edit but still it is not saving in the file.
– Tayyab Gulsher Vohra
Nov 29 '18 at 6:05
I am getting the same error as before 'str' object cannot be interpreted as an integer
– Tayyab Gulsher Vohra
Nov 29 '18 at 5:47
I am getting the same error as before 'str' object cannot be interpreted as an integer
– Tayyab Gulsher Vohra
Nov 29 '18 at 5:47
Updated the answer
– Shivam Singh
Nov 29 '18 at 5:56
Updated the answer
– Shivam Singh
Nov 29 '18 at 5:56
Shivam the error has been eliminated but nothing is saving the file...
– Tayyab Gulsher Vohra
Nov 29 '18 at 6:00
Shivam the error has been eliminated but nothing is saving the file...
– Tayyab Gulsher Vohra
Nov 29 '18 at 6:00
You need to fix the split function, It should be
str.split(new_host, "/")[6] and BASE_URL = "http://espncricinfo.com"– Shivam Singh
Nov 29 '18 at 6:01
You need to fix the split function, It should be
str.split(new_host, "/")[6] and BASE_URL = "http://espncricinfo.com"– Shivam Singh
Nov 29 '18 at 6:01
Shivam done both of these above edit but still it is not saving in the file.
– Tayyab Gulsher Vohra
Nov 29 '18 at 6:05
Shivam done both of these above edit but still it is not saving in the file.
– Tayyab Gulsher Vohra
Nov 29 '18 at 6:05
|
show 2 more comments
The problem is your print statement.
It should read
print('checking %d etc.' % (i + 1))
its working i have checked i have also removed this line but the error remains same..
– Tayyab Gulsher Vohra
Nov 29 '18 at 5:41
What line number, doesnew_hostas a class have a__str__method. Youprintit and usestr.splitin the final line
– rhubarbdog
Nov 29 '18 at 5:47
its a variable..
– Tayyab Gulsher Vohra
Nov 29 '18 at 5:50
What doessoupy.findall('a', { ... })return. A range or a list of strings. You setnew_hosttoBASE_URL + new_host. After setting it to a dictionary lookupnew_host['href']
– rhubarbdog
Nov 29 '18 at 5:58
add a comment |
The problem is your print statement.
It should read
print('checking %d etc.' % (i + 1))
its working i have checked i have also removed this line but the error remains same..
– Tayyab Gulsher Vohra
Nov 29 '18 at 5:41
What line number, doesnew_hostas a class have a__str__method. Youprintit and usestr.splitin the final line
– rhubarbdog
Nov 29 '18 at 5:47
its a variable..
– Tayyab Gulsher Vohra
Nov 29 '18 at 5:50
What doessoupy.findall('a', { ... })return. A range or a list of strings. You setnew_hosttoBASE_URL + new_host. After setting it to a dictionary lookupnew_host['href']
– rhubarbdog
Nov 29 '18 at 5:58
add a comment |
The problem is your print statement.
It should read
print('checking %d etc.' % (i + 1))
The problem is your print statement.
It should read
print('checking %d etc.' % (i + 1))
answered Nov 29 '18 at 5:40
rhubarbdogrhubarbdog
37119
37119
its working i have checked i have also removed this line but the error remains same..
– Tayyab Gulsher Vohra
Nov 29 '18 at 5:41
What line number, doesnew_hostas a class have a__str__method. Youprintit and usestr.splitin the final line
– rhubarbdog
Nov 29 '18 at 5:47
its a variable..
– Tayyab Gulsher Vohra
Nov 29 '18 at 5:50
What doessoupy.findall('a', { ... })return. A range or a list of strings. You setnew_hosttoBASE_URL + new_host. After setting it to a dictionary lookupnew_host['href']
– rhubarbdog
Nov 29 '18 at 5:58
add a comment |
its working i have checked i have also removed this line but the error remains same..
– Tayyab Gulsher Vohra
Nov 29 '18 at 5:41
What line number, doesnew_hostas a class have a__str__method. Youprintit and usestr.splitin the final line
– rhubarbdog
Nov 29 '18 at 5:47
its a variable..
– Tayyab Gulsher Vohra
Nov 29 '18 at 5:50
What doessoupy.findall('a', { ... })return. A range or a list of strings. You setnew_hosttoBASE_URL + new_host. After setting it to a dictionary lookupnew_host['href']
– rhubarbdog
Nov 29 '18 at 5:58
its working i have checked i have also removed this line but the error remains same..
– Tayyab Gulsher Vohra
Nov 29 '18 at 5:41
its working i have checked i have also removed this line but the error remains same..
– Tayyab Gulsher Vohra
Nov 29 '18 at 5:41
What line number, does
new_host as a class have a __str__ method. You print it and use str.split in the final line– rhubarbdog
Nov 29 '18 at 5:47
What line number, does
new_host as a class have a __str__ method. You print it and use str.split in the final line– rhubarbdog
Nov 29 '18 at 5:47
its a variable..
– Tayyab Gulsher Vohra
Nov 29 '18 at 5:50
its a variable..
– Tayyab Gulsher Vohra
Nov 29 '18 at 5:50
What does
soupy.findall('a', { ... }) return. A range or a list of strings. You set new_host to BASE_URL + new_host. After setting it to a dictionary lookup new_host['href']– rhubarbdog
Nov 29 '18 at 5:58
What does
soupy.findall('a', { ... }) return. A range or a list of strings. You set new_host to BASE_URL + new_host. After setting it to a dictionary lookup new_host['href']– rhubarbdog
Nov 29 '18 at 5:58
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.
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%2f53532368%2fstr-object-cannot-be-interpreted-as-an-integer-in-python%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
Where are you getting the error? (which line?)
– Tomothy32
Nov 29 '18 at 5:25
last two lines i am getting error
– Tayyab Gulsher Vohra
Nov 29 '18 at 5:26
What is BASE_URL?
– Shivam Singh
Nov 29 '18 at 5:36
You're writing in byte mode (
"wb"), but I'm guessing you're trying to writestrdata rather thanbytes. What happens if you changerequests.get(new_host).texttorequests.get(new_host).text.encode()?– JaminSore
Nov 29 '18 at 5:39
@ShivamSingh BASE_URL = 'espncricinfo.com'
– Tayyab Gulsher Vohra
Nov 29 '18 at 5:39