Skip to content

Instantly share code, notes, and snippets.

@heykush
Last active February 5, 2023 06:30
Show Gist options
  • Save heykush/2e00cd37e254b3d8b12060191ac37378 to your computer and use it in GitHub Desktop.
Save heykush/2e00cd37e254b3d8b12060191ac37378 to your computer and use it in GitHub Desktop.
Auto Posting script for blogger using python.
from bs4 import BeautifulSoup
import requests
import re
import subprocess
import random
product_link = "https://www.amazon.in/dp/B09ZTZ9N3Q"
# get the page content
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0'}
res = requests.get(product_link)
aa = res.url
# print(aa)
page = requests.get(aa, headers=headers)
# print(page.status_code)
soup = BeautifulSoup(page.content, 'html.parser')
# get id of the element you want to scrape
idl = soup.find(id="imgTagWrapperId")
idp = soup.find(id="productTitle")
idd = soup.find(id="feature-bullets")
# get the text
product_title = idp.text.strip()
#words list about product quality
good=['Great','Strong', 'Good', 'Excellent', 'Best', 'Nice', 'Perfect', 'Awesome', 'Super', 'Superb', 'Quality', 'Value', 'Worth']
product_description = idd.text.strip().replace('About this item', f'{random.choice(good)} Product to think about buying.')
# print(product_title)
# print(product_description)
# get first url from data-a-dynamic-image attribute
for i in idl.find_all('img'):
a = i['data-a-dynamic-image'].split('"')[1]
asn = re.sub('\_[\w]+', '_SX1000_', a)
product_image = asn
pro_des = []
for i in product_description.splitlines():
result = re.split(r'\s{2,}', i)
random.shuffle(result)
for j in result:
lis = f"<li><span>{j}</span></li>"
pro_des.append(lis)
prod_des = ''.join(pro_des)
html_content = f'''<div>
<a href={product_image}
style="display: block; padding: 1em 0px; text-align: center;"><img border="0" height="200"
src={product_image}></a>
</div>&nbsp;
<div>
<a href={product_link} target="_blank"
style="display: block; padding: 1em 0px; text-align: center;"><img border="0" height="150"
src="https://blogger.googleusercontent.com/img/a/AVvXsEi3byHlsY-f8mGsZbPuAfoZpsaGNVY8HnDNuZpFs-bGuhmS70vL1JQ640FooWNNg3ErYneeugluXZy2R5z01OAcWx65pNr6g0AisXjKtw7IO_uDo7bHfBz_NAfqYcBYU2_qAatigH_b0yByLHUgJk4uM6aMagLnwxNWOboX1C7A9FhcMSuw4zL1I2j0"
width="300">
</a>
</div>
<p><span style="font-size: 15px; color: red"><strong>*Product price and availability are accurate at the time of posting the deal and are subject to change.</strong></span></p>
<p><span style="font-size: 20px;"><strong>Product Description: </strong></span></p>
<ul >
{prod_des}
</ul>
</div>'''
# print(html_content)
# write a html file with the html content
with open('blog.html', 'w') as f:
f.write(html_content)
f.close()
subprocess.call(
f'easyblogger.exe --blogid <blogid> post -t "{product_title}" --publish -f blog.html"', shell=True)
#delete the html file
subprocess.call('del blog.html', shell=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment