I have a simple Flask app that extracts post and profile id from the source code of a Facebook page. User enters the FB post's URL to input box, application makes a request to facebook to fetch the source, then process it with beautifulsoup. The rest is handled locally by regex.
def extract_post_link_from_posts(self, link):
link = link.replace("m.f", "www.f")
res = requests.get(link, headers = self.headers)
soup_data = BeautifulSoup(res.text, 'html.parser')
match = re.search(self.link_pattern, soup_data.prettify())
if match:
link = match.group()
link2 = link.strip('link href=').strip('"').split('/')
link3 = 'https://www.facebook.com/{}/posts/{}/'.format(link2[3],link2[6])
result = link3
return result
This is the function. When I use it locally on my PC it works just great. However, when I try to use it on my Pythonanywhere website, the request fails and requests.get only returns page source of login page. I use headers as you can see. I tried several things but can't get it to work.