簡體   English   中英

Python BeautifulSoup 和請求

[英]Python BeautifulSoup and Requests

每當我嘗試運行此代碼時:

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)

我使用的 url 是:

'https://www.adidas.com/us/stan-smith-shoes/FZ3815.html?forceSelSize=FZ3815_630'

model 是: '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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM