requests.get(url) 没有返回这个特定的 url [英] requests.get(url) not returning for this specific url

查看:96
本文介绍了requests.get(url) 没有返回这个特定的 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 requests.get(url).text 从该网站获取 HTML.但是,当使用此特定 url 调用 requests.get(url) 时,无论我等待多长时间,它都不会返回.这适用于其他网址,但这个特别给我带来了麻烦.代码如下

I'm trying to use requests.get(url).text to get the HTML from this website. However, when requests.get(url) is called with this specific url, it never returns no matter how long I wait. This works with other urls, but this one specifically is giving me trouble. Code is below

from bs4 import BeautifulSoup
import requests

source = requests.get('https://www.carmax.com/cars/all', allow_redirects=True).text

soup = BeautifulSoup(source, 'lxml')

print(soup.prettify().encode('utf-8'))

感谢您的帮助!

推荐答案

尝试:

import requests
from bs4 import BeautifulSoup

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Safari/537.36', "Upgrade-Insecure-Requests": "1","DNT": "1","Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","Accept-Language": "en-US,en;q=0.5","Accept-Encoding": "gzip, deflate"}
html = requests.get("https://www.carmax.com/cars/all",headers=headers)
soup = BeautifulSoup(html.content, 'html.parser')
print(soup.prettify())

这篇关于requests.get(url) 没有返回这个特定的 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆