Task4链接
https://github.com/datawhalechina/team-learning/tree/master/Python%E7%88%AC%E8%99%AB%E7%BC%96%E7%A8%8B%E5%AE%9E%E8%B7%B5/task4
Task4算是对前几天学习内容的一个总结,说实话前面的内容还没完全理解,囫囵吞枣搞了一通
1.腾讯新闻
爬取腾讯新闻基本上把前几天的代码整合一下就行
主要用到webdriver,BeautifulSoup等几个库
import time
from selenium import webdriver
driver=webdriver.Chrome(executable_path="D:\chromedriver\chromedriver.exe")
driver.get("https://news.qq.com")
#了解ajax加载
for i in range(1,100):
time.sleep(2)
driver.execute_script("window.scrollTo(window.scrollX, %d);"%(i*200))
from bs4 import BeautifulSoup
html=driver.page_source
bsObj=BeautifulSoup(html,"lxml")
jxtits=bsObj.find_all("div",{"class":"jx-tit"})[0].find_next_sibling().find_all("li")
print("index",",","title",",","url")
for i,jxtit in enumerate(jxtits):
# print(jxtit)
try:
text=jxtit.find_all("img")[0]["alt"]
except:
text=jxtit.find_all("div",{"class":"lazyload-placeholder"})[0].text
try:
url=jxtit.find_all("a")[0]["href"]
except:
print(jxtit)
print(i+1,",",text,",",url)
2.知乎爬取
有空再补吧