为什么我在美汤里找不到这个标签?
Why cant i find this tag in beautiful soup?
我是 python 的新手,或与此相关的任何合成语言,但我试图使用此代码从网站上抓取标题,但它一直打印 "None" 就像标题一样,或者任何标签,如果我替换它,不存在。
import bs4
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
my_url = "https://www.roblox.com/catalog/?CatalogContext=1&Keyword=the%20item&SortAggregation=5&LegendExpanded=true&Category=2"
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
page_soup = soup(page_html, "html.parser")
ttt = page_soup.find("div", {"class":"CatalogItemName notranslate"})
item = ttt.a.text
print(item)
当您想使用多个 类 查找元素时,我认为以下是惯例。
soup.find("div", {'class':['CatalogItemName', 'notranslate']})
您要查找的内容不在从服务器收到的 http 响应中。页面加载后,它由 javascript 生成。
执行抓取任务时,您应该始终在浏览器中加载网站而不 javascript,以便更好地了解原始 html 内容的外观。
最后,您可以使用像 selenium 这样具有 javascript 支持的爬虫来解决这个问题。
我是 python 的新手,或与此相关的任何合成语言,但我试图使用此代码从网站上抓取标题,但它一直打印 "None" 就像标题一样,或者任何标签,如果我替换它,不存在。
import bs4
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
my_url = "https://www.roblox.com/catalog/?CatalogContext=1&Keyword=the%20item&SortAggregation=5&LegendExpanded=true&Category=2"
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
page_soup = soup(page_html, "html.parser")
ttt = page_soup.find("div", {"class":"CatalogItemName notranslate"})
item = ttt.a.text
print(item)
当您想使用多个 类 查找元素时,我认为以下是惯例。
soup.find("div", {'class':['CatalogItemName', 'notranslate']})
您要查找的内容不在从服务器收到的 http 响应中。页面加载后,它由 javascript 生成。
执行抓取任务时,您应该始终在浏览器中加载网站而不 javascript,以便更好地了解原始 html 内容的外观。
最后,您可以使用像 selenium 这样具有 javascript 支持的爬虫来解决这个问题。