Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
820 views
in Technique[技术] by (71.8m points)

scraping a fake <a> with python

I'm a beginner in Python, and I'm using BeautifullSoup to scrape data from an html page.

So far, everything fine. But some links are weird, and perhaps the purpose is not to be scraped. This page : https://francechansons.net/alain-souchon-liste-de-chansons/ has a list of links, with href being themselves links, instead of url. My current code is :

from urllib.request import urlopen
from bs4 import BeautifulSoup as bs

html_page = urlopen('https://francechansons.net/alain-souchon-liste-de-chansons/')
soup = bs(html_page, 'lxml')

entry_content_div=soup.find("div", class_="entry-content") 
ul = entry_content_div.find("ul")
li = ul.find('li')
children = li.findChildren("a")
for child in children:
    print(child)

I get

 <a href="alain_souchon-18_ans_que_j_t_ai_a_l_oeil">18 ans que j’t’ai à l’?il</a>

instead of :

<a href="https://francechansons.net/alain_souchon-18_ans_que_j_t_ai_a_l_oeil/">18 ans que j’t’ai à l’?il</a>'

Hope someone understands this convoluted message

question from:https://stackoverflow.com/questions/65937565/scraping-a-fake-a-with-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just concatenate the href with a base url like this:

baseUrl = 'https://francechansons.net/'
    for child in children:
        print(baseUrl+child['href'])

But check if there is already an http in the href:

if 'http' in child['href']:
    print(child['href'])
else:
    print(baseUrl+child['href'])

Example

from urllib.request import urlopen
from bs4 import BeautifulSoup as bs

html_page = urlopen('https://francechansons.net/alain-souchon-liste-de-chansons/')
soup = bs(html_page, 'lxml')

entry_content_div=soup.find("div", class_="entry-content") 
ul = entry_content_div.find("ul")
li = ul.find('li')
children = li.findChildren("a")
baseUrl = 'https://francechansons.net/'
for child in children:
    if 'http' in child['href']:
        print(child['href'])
    else:
        print(baseUrl+child['href'])

Output

https://francechansons.net/alain_souchon-18_ans_que_j_t_ai_a_l_oeil


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...