В Python 3 у меня есть эта программа для извлечения таблицы с сайта с использованием lxml, а затем создается фрейм данных (на основе Syed Sadat Nazrul's - https://towardsdatascience.com/web-scraping-html-tables-with-python-c9baba21059):
import requests
import lxml.html as lh
import pandas as pd
# Sample site where the table is
response = requests.get('https://especiais.gazetadopovo.com.br/futebol/tabela-campeonato-brasileiro-2018')
#Store the contents of the website under doc
doc = lh.fromstring(response.content)
#Parse data that are stored between <tr>..</tr> of HTML
tr_elements = doc.xpath('//tr')
col=[]
i=0
#For each row, store each first element (header) and an empty list
for t in tr_elements[0]:
i+=1
name=t.text_content()
col.append((name,[]))
#Since out first row is the header, data is stored on the second row onwards
for j in range(1,len(tr_elements)):
#T is our j'th row
T=tr_elements[j]
#If row is not of size 10, the //tr data is not from our table
if len(T)!=10:
break
#i is the index of our column
i=0
#Iterate through each element of the row
for t in T.iterchildren():
data=t.text_content()
#Check if row is empty
if i>0:
#Convert any numerical value to integers
try:
data=int(data)
except:
pass
#Append the data to the empty list of the i'th column
col[i][1].append(data)
#Increment i for the next column
i+=1
# Creates the dataframe
Dict={title:column for (title,column) in col}
df=pd.DataFrame(Dict)
Но это таблица с href в одном из столбцов, в первом столбце, который в таблице не имеет названия:
<td class="campeao times link-time"><a href="https://especiais.gazetadopovo.com.br/futebol/times/palmeiras/">Palmeiras</a></td>
Итак, я хотел бы извлечь href из каждой строки и добавить в столбец базы данных:
P J V E D GP GC SG Link
0 Palmeiras 80 38 23 11 4 64 26 38 https://especiais.gazetadopovo.com.br/futebol/times/palmeiras/
1 Flamengo 72 38 21 9 8 59 29 30 https://especiais.gazetadopovo.com.br/futebol/times/flamengo/
...
Пожалуйста, итерация в "iterchildren" принимает тексты с "text_content". Есть ли способ также получить встроенную ссылку href?