抓取的内容与我在浏览器检查器中看到的不同 - Python scraper with Selenium
The scraped content is different from what I see in browser Inspector - Python scraper with Selenium
我想抓取此网站 https://lens.zhihu.com/api/v4/videos/1123764263738900480 以使用 Python 获取 play_url
。
此网站的重定向非常快,url 没有变化。原页面中的play_url
是无效的,如果你想访问它,你会看到“你没有权限...”。所以我在程序中使用 time.sleep(10)
来处理重定向(这似乎不适用于 Requests)。
(抱歉,我弄错了,我看到的重定向过程可能只是我的Firefox浏览器导致的。但是我说的方法确实可以处理重定向。)
但是我在程序中的1.txt
中看到,抓取的内容没有我想要的play_url
,其中的url仍然无效
这里是我想要的play_url
,在浏览器Inspector中可以看到标签a
,其class
的值为url
:image
这是我使用的代码:
import time
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
service = Service(executable_path='C:\Users\X\chromedriver_win32\chromedriver.exe')
options = Options()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_argument("--headless")
options.add_argument('user-agent="Mozilla/5.0"')
driver = webdriver.Chrome(service=service, options=options)
driver.get('https://lens.zhihu.com/api/v4/videos/1123764263738900480')
time.sleep(10)
pageSource = driver.page_source
driver.quit()
bs = BeautifulSoup(pageSource, 'html.parser')
with open('C:\Users\X\Desktop\1.txt', 'a', encoding='utf-8') as file:
file.write(f"{bs}")
play_url = bs.find('a', {'class': 'url'}).get("title")
print(play_url)
它 returns:
Traceback (most recent call last):
File "c:\Users\X\Desktop\Handle redirect\Whosebug.py", line 22, in <module>
play_url = bs.find('a', {'class': 'url'}).get("title")
AttributeError: 'NoneType' object has no attribute 'get'
所以在抓取的内容中,没有我在浏览器检查器中看到的('a', {'class': 'url'})
。
为什么抓取的内容与我在浏览器检查器中看到的不同,如何处理?
编辑: 感谢 Martin Evans 的评论,现在我知道浏览器从源代码处理 Javascript,所以它看起来与源代码不同。但就我而言,我在开发人员工具网络中看不到任何 js 链接。实际上,只有两个链接:image。所以我对上面的问题还是一头雾水
更新: 感谢@Sarhan 的评论,我解决了这个问题。我以前使用 Firefox,即使标签不存在,浏览器也会自动呈现源代码。我在 Edge 中尝试 url,但根本没有 ('a', {'class': 'url'})
。此外,非常感谢@Dimitar,所以我可以获得 play_url.
您可以使用 Requests 从 url 获取响应并提取 play_url。
import requests
url = "https://lens.zhihu.com/api/v4/videos/1123764263738900480"
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36"
}
response = requests.get(url, headers=headers)
data = response.json()
play_url = data['playlist']['LD']['play_url']
print(play_url)
我想抓取此网站 https://lens.zhihu.com/api/v4/videos/1123764263738900480 以使用 Python 获取 play_url
。
此网站的重定向非常快,url 没有变化。原页面中的play_url
是无效的,如果你想访问它,你会看到“你没有权限...”。所以我在程序中使用 time.sleep(10)
来处理重定向(这似乎不适用于 Requests)。
(抱歉,我弄错了,我看到的重定向过程可能只是我的Firefox浏览器导致的。但是我说的方法确实可以处理重定向。)
但是我在程序中的1.txt
中看到,抓取的内容没有我想要的play_url
,其中的url仍然无效
这里是我想要的play_url
,在浏览器Inspector中可以看到标签a
,其class
的值为url
:image
这是我使用的代码:
import time
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
service = Service(executable_path='C:\Users\X\chromedriver_win32\chromedriver.exe')
options = Options()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_argument("--headless")
options.add_argument('user-agent="Mozilla/5.0"')
driver = webdriver.Chrome(service=service, options=options)
driver.get('https://lens.zhihu.com/api/v4/videos/1123764263738900480')
time.sleep(10)
pageSource = driver.page_source
driver.quit()
bs = BeautifulSoup(pageSource, 'html.parser')
with open('C:\Users\X\Desktop\1.txt', 'a', encoding='utf-8') as file:
file.write(f"{bs}")
play_url = bs.find('a', {'class': 'url'}).get("title")
print(play_url)
它 returns:
Traceback (most recent call last):
File "c:\Users\X\Desktop\Handle redirect\Whosebug.py", line 22, in <module>
play_url = bs.find('a', {'class': 'url'}).get("title")
AttributeError: 'NoneType' object has no attribute 'get'
所以在抓取的内容中,没有我在浏览器检查器中看到的('a', {'class': 'url'})
。
为什么抓取的内容与我在浏览器检查器中看到的不同,如何处理?
编辑: 感谢 Martin Evans 的评论,现在我知道浏览器从源代码处理 Javascript,所以它看起来与源代码不同。但就我而言,我在开发人员工具网络中看不到任何 js 链接。实际上,只有两个链接:image。所以我对上面的问题还是一头雾水
更新: 感谢@Sarhan 的评论,我解决了这个问题。我以前使用 Firefox,即使标签不存在,浏览器也会自动呈现源代码。我在 Edge 中尝试 url,但根本没有 ('a', {'class': 'url'})
。此外,非常感谢@Dimitar,所以我可以获得 play_url.
您可以使用 Requests 从 url 获取响应并提取 play_url。
import requests
url = "https://lens.zhihu.com/api/v4/videos/1123764263738900480"
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36"
}
response = requests.get(url, headers=headers)
data = response.json()
play_url = data['playlist']['LD']['play_url']
print(play_url)