使用python3.6和BeautifulSoup。我得到的是href,但是我不能下载它们

2024-10-03 13:25:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从html源下载文件。 e、 g

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

我得到了所有的href,但是我试图得到href的实际内容

下面的代码得到了上面的(很多),并且做得很快。如何获取这些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)

Tags: thefromimport内容gethtmlurllibrequests
1条回答
网友
1楼 · 发布于 2024-10-03 13:25:48

您必须在循环中调用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)

相关问题 更多 >