Python Forum
Inconsistent behaviour in output - web scraping
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Inconsistent behaviour in output - web scraping
#1
Hi all,

I'm not an expert coding in python and have used it a few times to write things like the following example code which is meant to extract some information via scrapping from Yahoo finance.

The code is:

from bs4 import BeautifulSoup
import requests as req
import re
import urllib3
import sys

# Parse input command line parameters.
if (len(sys.argv) > 1):
    sStockTicker = sys.argv[1] 
else:
    # TODO: Change this to uncomment the exit when debug is finished
    print('Invalid usage. A stock ticker must be passed as parameter.')
    sStockTicker='FMG.AX'
    #sys.exit(2)

url='https://finance.yahoo.com/quote/' + sStockTicker 

req = urllib3.PoolManager()
res = req.request("GET", url)
soup3 = BeautifulSoup(res.data,'lxml')
print (soup3.find(id="quote-header-info").contents[2].contents[0].contents[0].contents[0].text)
Now, depending on what stock I run this for (the parameter), the code will generate data the has the same content as the web page that I would see via accessing the same URL in a web browser, such as Chrome/Firefox, and then extract the stock price. It does not always do this though. If I run the script for "IBM", it will work fine. If I run it for "FMG.AX" it will work fine. However, if I run it for "IOZ.AX" it will fail. If I paste the same URL into the web browser, it will load perfectly fine and show the expected results.

Hence - my interpretation of the issue - the code is pulling back a different result using urllib3 than I get from the browser, and I'm a little baffled as to why. I assume the web server is noticing it is being called from Python somehow based on the values in the request, and then returning a different response.

Does anyone know why this is occurring and how I can work around it? Is it actually caused for the reason I said above? I am actually very interested in this intellectually as well as resolving the issue in the script. I find it very odd that a server script would respond in different ways purposefully like this (if that is indeed the cause).

TIA!

Steve.
Reply


Messages In This Thread
Inconsistent behaviour in output - web scraping - by Steve - Sep-18-2021, 10:25 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  logger behaviour setdetnet 1 899 Apr-15-2023, 05:20 AM
Last Post: Gribouillis
  can someone explain this __del__ behaviour? rjdegraff42 1 732 Apr-12-2023, 03:25 PM
Last Post: deanhystad
  Asyncio weird behaviour vugz 2 1,266 Apr-09-2023, 01:48 AM
Last Post: vugz
  Read csv file with inconsistent delimiter gracenz 2 1,208 Mar-27-2023, 08:59 PM
Last Post: deanhystad
  Weird behaviour using if statement in python 3.10.8 mikepy 23 3,647 Jan-18-2023, 04:51 PM
Last Post: mikepy
  Inconsistent loop iteration behavior JonWayn 2 1,007 Dec-10-2022, 06:49 AM
Last Post: JonWayn
  Generator behaviour bla123bla 2 1,111 Jul-26-2022, 07:30 PM
Last Post: bla123bla
  ValueError: Found input variables with inconsistent numbers of samples saoko 0 2,484 Jun-16-2022, 06:59 PM
Last Post: saoko
  Loop Dict with inconsistent Keys Personne 1 1,613 Feb-05-2022, 03:19 AM
Last Post: Larz60+
  Inconsistent counting / timing with threading rantwhy 1 1,775 Nov-24-2021, 04:04 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020