trying to download some pdf files from the website using requests module, but I keep getting this error listed below. I saw several posts where they mentioned to use response.content for pdf files instead of response.text, but it's still generating error. Not sure how to fix this.
example link: https://corporate.exxonmobil.com/-/media/Global/Files/worldwide-giving/2018-Worldwide-Giving-Report.pdf
def scrape_website(link):
try:
print("getting content")
cert = requests.certs.where()
page = requests.get(link, verify=cert, headers={"User-Agent": "Mozilla/5.0 (X11; CrOS x86_64 12871.102.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.141 Safari/537.36"})
print(page)
if ".pdf" in link:
print("the content is a pdf file. downloading..")
return page.content
return page.text
except Exception as x:
print(x)
return ''
statement_page = scrape_website(link)
with open(filepath, 'w+', encoding="utf-8") as f:
print("writing page")
f.write(statement_page)
f.close()
<ipython-input-42-1e4771d32073> in save_html_page(page, path, filename)
13 with open(filepath, 'w+', encoding="utf-8") as f:
14 print("writing page")
---> 15 f.write(page)
16 f.close()
17
TypeError: write() argument must be str, not bytes
'w+'to'wb'statement_pagebut then try to writepageto filea bytes-like object is required, not 'str'