python - Python BeautifulSoup 和请求
问题描述
每当我尝试运行此代码时:
def CheckStock(url,model):
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
RawHTML = requests.get(url, headers=headers)
Page = bs4.BeautifulSoup(RawHTML.text, "lxml")
我不断得到:
requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='www.adidas.com', port=443): Read timed out. (read timeout=None)
我使用的网址是:
'https://www.adidas.com/us/stan-smith-shoes/FZ3815.html?forceSelSize=FZ3815_630'
型号为:'FZ3815'
解决方案
要获得正确的页面,请指定不同的User-Agent
.
例如:
import requests
from bs4 import BeautifulSoup
url = 'https://www.adidas.com/us/stan-smith-shoes/FZ3815.html?forceSelSize=FZ3815_630'
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0'}
RawHTML = requests.get(url, headers=headers)
Page = BeautifulSoup(RawHTML.text, "lxml")
print(Page)
印刷:
<!DOCTYPE html>
<html class="theme-adidas" data-reactroot="" lang="en" prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb#"><head><title data-rh="true" id="meta-title">Stan Smith Tropical Print Sneakers | adidas US</title><meta charset="utf-8" data-rh="true" id="meta-charset"/><meta content="IE=edge,chrome=1" data-rh="true" http-equiv="X-UA-Compatible" id="meta-http-ua-compatible"/><meta content="text/html;charset=utf-8" data-rh="true" http-equiv="Content-Type" id="meta-http-content-type"/><meta content="
...and so on.
推荐阅读
- sql - 如何编写多列“in” sql 查询语法?
- assembly - movslq 是做什么的?
- javascript - 使用 VueX Store 中的计算属性进行 Ajax 调用的正确方法是什么
- pointers - 跳转指令后的十六进制数字如何转换为内存地址?
- html - 当我尝试重复 x 一个图像精灵时,它会显示整个图像
- gtk - 拖动后获取窗口位置
- python - 如何正确缩放 MDIconButton?
- docker - 如何使用同一网络中的 localhost 访问其他 docker 容器中的一个容器的端口
- java - 如何将我的while循环的每个循环的速度设置为恒定?
- linux - 获取 java.lang.noclassdeffounderror: org.springframework/amqp/core/corelation