Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 抓取网站未返回正确的源代码_Python_Web Scraping_Beautifulsoup_Python Requests - Fatal编程技术网

Python 抓取网站未返回正确的源代码

Python 抓取网站未返回正确的源代码,python,web-scraping,beautifulsoup,python-requests,Python,Web Scraping,Beautifulsoup,Python Requests,我正试图用Python创建一个quizlet匹配集。我想用class:TermText 以下是URL:'https://quizlet.com/291523268" 导入请求 raw=请求.get(URL).text raw最终返回的东西根本不包含任何标签或卡片。当我检查网站的源代码时,它显示了我需要的所有术语文本跨度,这意味着它没有加载JS。因此,我不明白为什么我的HTML出现了错误,因为它不包含我需要的任何HTML。要从服务器获得正确的响应,请设置正确的用户代理HTTP头: import

我正试图用Python创建一个quizlet匹配集。我想用
class
TermText

以下是URL:'https://quizlet.com/291523268"

导入请求
raw=请求.get(URL).text

raw
最终返回的东西根本不包含任何标签或卡片。当我检查网站的源代码时,它显示了我需要的所有
术语文本
跨度,这意味着它没有加载JS。因此,我不明白为什么我的HTML出现了错误,因为它不包含我需要的任何HTML。

要从服务器获得正确的响应,请设置正确的
用户代理
HTTP头:

import requests
from bs4 import BeautifulSoup


url = 'https://quizlet.com/291523268/python-flash-cards/'
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:79.0) Gecko/20100101 Firefox/79.0'}

soup = BeautifulSoup(requests.get(url, headers=headers).content, 'html.parser')

for span in soup.select('span.TermText'):
    print(span.get_text(strip=True))
印刷品:

algorithm
A set of specific steps for solving a category of problems
token
basic elements of a language(letters, numbers, symbols)
high-level language
A programming language like Python that is designed to be easy for humans to read and write.
low-level langauge

...and so on.

为什么你需要发送用户代理@AndrejKesely@AaravM4没有用户代理,您将获得Clouflare验证码页面。当我从服务器获取这些类型的页面时,我将用户代理设置为第一件事。