用到的库:BeautifulSoup; pandas; bs4;Workbook
刚开始面临的问题:
1.不能准确定位到标签项,一直在转悠呢就是找不到,这样的情况可以选择直接print出来你的结果,如果是[]空的那肯定是错了,再找找。
2.标签找对了,输出的信息是一串串信息,太杂了,需要只把你需要的信息拿出来怎么办?我的办法是切片就行[:]直接切片操作。
3.到这里已经爬到了,但是没有保存到电脑,我刚开始一直无法保存,突然发现自己的循环写错地方了,需要把with open()写在最前面才可以写数据功能。
4.虽然大佬看我的代码太LOW了,或者看起来很简单,但是实际操作总会遇到问题,如果你和我一样你可以借鉴我的去自己动手试试。
import requests, bs4
from bs4 import BeautifulSoup
from openpyxl import Workbook
with open("book.csv","w",encoding="utf-8",newline="") as f: #这个是写数据的代码
page = 2010
while (page < 2022):
url = "https://proceedings.neurips.cc/paper_files/paper" + "/" + str(page) #网址
print(url)
res = requests.get(url).text #获取页面html源码
soup = BeautifulSoup(res, 'lxml') #通过BeautifulSoup解析页面
list=soup.find_all('li',class_='none')[:] #获取列表的页面
page += 1
for res1 in list:
list1 = (res1.find_all(title="paper title")[0]) #定位到我需要的地方
print(list1.string) #需要转为string类型的存储
f.write(list1.string) #写数据
f.write("\n") #这是为了换行