Other answers have already covered that you absolutely can visit a healthcare professional, be prescribed drugs, and purchase them.
The notes about cost and coverage are pretty important.
A travel insurance policy is a very good idea for this reason; be sure to look carefully at the coverage provided like dollar and time coverage limits, but also whether prescriptions are covered at all (some plans do and some don't). Additionally, most of these policies of course have provisions against covering "pre-existing" conditions so if these are regular medications you already know you need, for an existing condition, they probably won't be covered in any case.
While buying local coverage directly is often ridiculously expensive, there are subsidized options available for temporary residents sometimes.
You mention staying for an "extended period" but not via citizenship or permanent residency, so I assume "extended" for you means either as a tourist or on a non-immigrant visa such as a student (F) visa.
Some states do have medical coverage options for immigrants and non-immigrant visa holders. Options typically require that you are legally present and documented.
I know New York state best so that's the only examples I will give, but it will be helpful to look at the states where you are staying.
New York state
New York State lays out its immigration requirements for public coverage in this document. You would fall under Category 4 most likely, and thus be ineligible for anything but emergency care.
However, New York also has a health care marketplace called New York State of Health, and one of the options there for low/no-income applicants is called the Essential plan, and non-immigrant visa holders (such as foreign students) are eligible for this plan.
This page lays out various coverage options for immigrants (it's a New York City website but I think it applies to New York state generally).
More details about the Essential plan itself are in this document.
While the marketplace generally has an open enrollment period (a time of year when enrollment is open, and otherwise not available), the Essential plan can be enrolled into at any time.
Assuming you meet the income and other eligibility criteria, your monthly cost will either be $0 or $20. Other fees like co-pays (a small amount you pay for each doctor visit, or each prescription drug) may also be included, or may be $0 (anecdotally, those who I've helped apply for the program have paid nothing ever for any of the coverage, however they also had no income at all).
Unlike the health coverage most Americans have, this plan also covers dental and vision.
The one thing to be aware of is that this plan will only cover in-network providers. That means that only healthcare providers who have a pre-existing contract with the insurance company who backs this plan, will be covered by the plan. Depending on where you are from, this system, like much of US healthcare, will be confusing and probably surprising. The important thing to note is to check that any place you are receiving care accepts not only this insurance company, but the specific plan (it will not be accepted as readily as some other plans, even other plans from the same company).
This is not usually difficult to determine when you are seeing a single provider, and planning it in advance; it can mostly be a problem when going to a large hospital, especially for something unplanned like an emergency, because in large hospitals, not everyone you see at the hospital is part of the hospital for billing purposes, and may be out of network. I won't belabor this point more but it's something to research if you go this route.