Why did python3 get html when collecting this page but not when fetching an element?

import requests
from bs4 import BeautifulSoup


if __name__ == "__main__":

    headers = {    
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36 QQBrowser/4.4.106.400",    
    "Connection": "keep-alive",       
    "Accept-Language": "zh-CN,zh;q=0.9",
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"}

    html=requests.get("https://car.autohome.com.cn/price/brand-33.html",headers=headers)
    html.encoding = "gb2312"
    soup = BeautifulSoup(html.text,"lxml")
    -sharp
    xx=soup.find("h2","class_=fn-left name") 
    yy=xx.find("a")
    print(yy["href"])  -sharph2
    """
    <h2 class="fn-left name"><a href="/price/brand-33.html"></a></h2>
    """```
    
Dec.28,2021

replace "lxml" with "html.parser". It feels like there's something wrong with bs4 and lxml.


you want to take the specific location information, did not find the < H2 class= "fn-left name" > tag information, you cut a picture to specify the location.


is probably your find problem, cars = soup.findAll ('H2, {'class':' fn-left name'}) , and then iterate through cars .

Menu