我是一个完全的初学者,只构建过基本的Python项目。现在,我正在用bs4用Python构建一个刮板,帮助我阅读网站上的成功故事。这些成功的故事都在一个表中,所以我想我会找到一个html标记,表示table,并将包含整个表。
但是,它只是
from bs4 import BeautifulSoup
from urllib.request import Request, urlopen
import requests
req = Request('https://www.calix.com/about-calix/success-stories.html', headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).read()
soup = BeautifulSoup(webpage, "lxml")
soup.find("div", {"id": "content-calix-en-site-prod-home-about-calix-success-stories-jcr-content"})
print('div')
我已经看了几个关于如何使用bs4的教程,并且我已经成功地浏览了一些基本的网站,但是我能做的只是获得所有的html,而不是我需要的块(只是成功的故事)。
您正在打印“div”
请确保打印汤,因为当您发现汤中有内容时,汤就会更新。
您应该看看bs4文档。
soup.find("div", {"id": "content-calix-en-site-prod-home-about-calix-success-stories-jcr-content"})
在这里,您调用了soup.find()
,但您没有将结果保存到变量中,因此结果会丢失。
print('div')
这里打印的是文字字符串div
。我想那不是你的本意。
试试这样的方法:
div = soup.find("div", {"id": "..."})
print(div)