py自動車の家を這い出すニュースケース

1176 ワード

import requests
from bs4 import BeautifulSoup

response = requests.get("https://www.autohome.com.cn/news/")

# 1. content /text     
# print(response.content)  # content      
response.encoding = 'gbk'
# print(response.text)  # text        


soup  = BeautifulSoup(response.text,'html.parser')

# tag = soup.find(id='auto-channel-lazyload-article')  #      ,    
# h3 = tag.find(name='h3',class_ ='')  # class           ,          
# h3 = tag.find(name='h3',attrs= {'class':''})  #
# print(h3)

#     
li_list = soup.find(id='auto-channel-lazyload-article').find_all(name='li')

for li in li_list:
    title = li.find('h3') #    
    if not title:#    null,  
        continue
    title = title.text
    summary = li.find("p").text
    url = li.find("a").get('href')
    img = li.find("img").get('src')
    print(img)

    #     
    res = requests.get(img)
    file_name = "%s.jpg"%(title,)
    with open(file_name,'wb') as f:
        f.write(res.content) #      

もっと素晴らしい文章は王明昌のブログに注目してください.