使用python中的请求直接获取节的src代码

使用python中的请求直接获取节的src代码,python,python-3.x,web-scraping,python-requests,Python,Python 3.x,Web Scraping,Python Requests,我想只从网站获取一个部分的源代码,而不是整个页面,然后解析出该部分,因为比加载整个页面然后解析要快。我尝试将节链接作为url参数传递,但仍然得到整个页面 url = 'https://stackoverflow.com/questions/19012495/smooth-scroll-to-div-id-jquery/#answer-19013712' response = requests.get(url) print(response.text) 您不能直接使用requests a

我想只从网站获取一个部分的源代码,而不是整个页面,然后解析出该部分,因为比加载整个页面然后解析要快。我尝试将节链接作为url参数传递,但仍然得到整个页面

 url = 'https://stackoverflow.com/questions/19012495/smooth-scroll-to-div-id-jquery/#answer-19013712'
 response = requests.get(url)
 print(response.text) 

您不能直接使用requests api获取特定的节,但可以使用
beautifulsoup
实现此目的。 dataquest提供了一个小样本:

运行上述脚本将输出此html字符串

<html>
<head>
<title>A simple example page
</title>
</head>
<body>
<div>
<p class="inner-text first-item" id="first">
First paragraph.
</p><p class="inner-text">
Second paragraph.
</p></div>
<p class="outer-text first-item" id="second"><b>
First outer paragraph.
</b></p><p class="outer-text"><b>
Second outer paragraph.
</b>
</p>
</body>
</html>
按类别划分:

soup.find_all('p', class_='outer-text')
按Id:

soup.find_all(id="first")


您不能直接使用requests api获取特定的节,但可以使用
beautifulsoup
实现此目的。 dataquest提供了一个小样本:

运行上述脚本将输出此html字符串

<html>
<head>
<title>A simple example page
</title>
</head>
<body>
<div>
<p class="inner-text first-item" id="first">
First paragraph.
</p><p class="inner-text">
Second paragraph.
</p></div>
<p class="outer-text first-item" id="second"><b>
First outer paragraph.
</b></p><p class="outer-text"><b>
Second outer paragraph.
</b>
</p>
</body>
</html>
按类别划分:

soup.find_all('p', class_='outer-text')
按Id:

soup.find_all(id="first")


HTTPS将不允许您这样做

您可以使用。您可以传递答案id 19013712。因此,只有通过API才能得到具体的答案


请注意,您可能仍然必须

HTTPS将不允许您这样做

您可以使用。您可以传递答案id 19013712。因此,只有通过API才能得到具体的答案


注意,您可能仍然必须

不可能,HTTP(S)协议不允许这样做。无论如何,服务器都会向您发送整个html文件,如果您可以控制服务器,那么您可以使用查询参数在发送到服务器端之前预过滤html文件不可能,HTTP(S)协议不允许这样做。服务器会向您发送整个html文件,如果您可以控制服务器,那么您可以使用查询参数在html文件发送到服务器端之前对其进行预筛选。我想在其他没有任何API的地方使用这个概念。在问题中,我仅以stackoverflow为例,我想在其他没有API的地方使用这个概念。在问题中,我仅以stackoverflow为例。