Использование python3.6 и BeautifulSoup.Я получаю это hrefs, но я не могу их скачать - PullRequest
0 голосов
/ 04 января 2019

Я пытаюсь загрузить файлы из HTML-источника.например,

<a class="el" href="classcs1graphics_1_1Circle.html">Circle</a>
<a class="el" href="classcs1graphics_1_1Polygon.html">Polygon</a>

Я получаю все hrefs, но я пытаюсь получить фактическое содержимое href.

Код ниже получает вышеупомянутый (много из них) и делает это быстро,Как я могу получить содержимое этих ссылок?Заранее спасибо.

Эд

import urllib.request, urllib.error, urllib.parse
from lxml import html
import requests
from bs4 import BeautifulSoup

#get the data from the URL
udata = requests.get('http://www.cs1graphics.org/doc/1.0/hierarchy.html')

#feed it to BeautifulSoup
soup = BeautifulSoup(udata.text,'html.parser')

#get all the <a table records
number_list_items = soup.find_all('a')

#get the rows in the records
for li_row in number_list_items:
    print(li_row)

1 Ответ

0 голосов
/ 04 января 2019

Вы должны позвонить requests.get() в цикле, чтобы загрузить контент.он использует относительный URL, поэтому вы должны определить базовый URL

base_url = 'http://www.cs1graphics.org/doc/1.0/'
#get the data from the URL
udata = requests.get(base_url + 'hierarchy.html')

#feed it to BeautifulSoup
soup = BeautifulSoup(udata.text,'html.parser')

#get all the <a table records
number_list_items = soup.find_all('a')

# get the rows in the records
for li_row in number_list_items:
    fileName = li_row['href']
    url = base_url + fileName
    print(url)      
    udata = requests.get(url)
    with open(fileName, 'w') as f:
        f.write(udata.text)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...