Pulling the href from a link when web scraping using Python

2024/10/13 6:21:15

I am scraping from this page: https://www.pro-football-reference.com/years/2018/week_1.htm

It is a list of game scores for American Football. I want to open the link to the stats for the first game. The text displayed for said says "Final". My code so far...

from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup#assigning url
my_url = "https://www.pro-football-reference.com/years/2018/week_1.htm"# opening up connection, grabbing the page
raw_page = uReq(my_url)
page_html = raw_page.read()
raw_page.close()# html parsing
page_soup = soup(page_html,"html.parser")#find all games on page
games = page_soup.findAll("div",{"class":"game_summary expanded nohover"})link = games[0].find("td",{"class":"right gamelink"})
print(link)

When I run this i receive the following output...

<a href="/boxscores/201809060phi.htm">Final</a>

How do I assign only the link text (i.e. "/boxscores/201809060phi.htm") to a variable?

Answer
link = games[0].find("td",{"class":"right gamelink"}).find('a')print(link['href'])
https://en.xdnf.cn/q/118114.html

Related Q&A

Php: Running a python script using blender from a php project using cmd commands

I need to run in cmd a python script for blender from blender and print the result from a php project, but I dont get the all result. Here is my code:$script = "C:\Users\madalina\Desktop\workspace…

Pymysql when executing Union query with %s (Parameter Placeholder)

This is the code about UNION QUERY:smith =Smithsmithb=Smithsql="""SELECT Distinct Pnumber FROM PROJECT, DEPARTMENT, EMPLOYEE WHERE Dnum = Dnumber AND Mgr_ssn=Ssn AND Lname= %s UNION SELE…

Django - Calling list or dict item using a variable in template

Im trying to call a dictionary or list object in a template using a variable in that template with no results.What Im trying to is identical to this general python code:keylist=[firstkey,secondkey,thir…

Multi-Classification NN with Keras error

I am getting an error when trying to do multi-classification with three classes. Error: TypeError: fit_generator() got multiple values for argument steps_per_epochCode Giving Error: NN.fit_generator(tr…

How to do time diff in each group on Pandas in Python

Heres the phony data:df = pd.DataFrame({email: [u1,u1,u1,u2,u2,u2],timestamp: [3, 1, 5, 11, 15, 9]})What I intend to retrieve is the time diff in each group of email. Thus, after sorting by timestamp i…

How to copy contents of a subdirectory in python

I am newbie to python, I am trying to achieve following task-I have a directory WP_Test containing a sub-directory test, I want to copy all the files and folders inside this sub-directory test to anoth…

Facing issue while providing dynamic name to file in python through a function

the line : with open(new%s.txt % intg ,a) as g : is giving error in below code. Every time I call the function "Repeat", it should create file with name new1.txt, new2.txt and so on. But it …

Python Pandas: Merging data frames on multiple conditions

I wish to merge data frames as fetched via sql under multiple condition. df1: First df contains Customer ID, Cluster ID and Customer Zone ID. The second df contain complain ID, registration number.…

counterpart to PILs Image.paste in PHP

I was asked to port a Python application to PHP (and Im not very fond of PHP).The part Im having trouble to port uses a set of monochromatic "template" images based on the wonderful Map Icons…

Google Cloud Dataflow fails in combine function due to worker losing contact

My Dataflow consistently fails in my combine function with no errors reported in the logs beyond a single entry of:A work item was attempted 4 times without success. Each time the worker eventually los…