爬取其乐社区最新主题

import requests
from bs4 import BeautifulSoup
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'}
res=requests.get('https://keylol.com/',headers=headers)
soup=BeautifulSoup(res.text,'html.parser')
o=soup.find('ol')
urls=[]
iiffmm=[]
for r in o.find_all("a",attrs={'title':True,'target':'_blank'}):
    href=r.get('href')
    urls.append('https://keylol.com/'+href)
def papa(url):
    inf=requests.get(url)
    infm=BeautifulSoup(inf.text,'html.parser')
    name=infm.find('a',attrs={'target':'_blank','class':'xw1'}).text.strip()
    article=infm.find('a',attrs={'id':'thread_subject'}).get('title').strip()
    
    iiffmm.append(name+'  '+article)

for url in urls:    
    try :
        papa(url)
    
    except AttributeError:
        pass
with open('a.txt','a+',encoding='utf-8') as f:
    for i in iiffmm:
        f.write(i+'\n')
    f.close()
    print('保存成功')

爬取其乐社区最新主题

上一篇:Python爬虫:设置Cookie解决网站拦截并爬取蚂蚁短租


下一篇:将Ldap组织结构及用户信息同步到MySQL,用Spring Boot项目操作