繁体   English   中英

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

[英]Scraping Website does not return correct source code

我试图用 Python 刮掉一个测验匹配集。 我想用class刮掉所有的<span>标签: TermText

这是 URL:'https://quizlet.com/291523268'

import requests
raw = requests.get(URL).text

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

要从服务器获得正确的响应,请设置正确User-Agent HTTP header:

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.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM