我试图为一个网站代码刮刀,到目前为止,我能够刮我需要的一般信息,但具体的属性值,我试图从该信息获得返回没有,即使有明显的价值那里。 在我尝试使用容器中每个容器的getattr来查找data-id的值之前,这一切都很正常。也许有更好的方法可以做到这一点,但是我很难理解为什么它找不到它。你知道吗
这就是我的代码。你知道吗
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup as soup
from selenium.webdriver.common.action_chains import ActionChains
url = "http://csgo.exchange/id/76561197999004010#x"
driver = webdriver.Firefox()
driver.get(url)
import time
time.sleep(10)
html = driver.page_source
soup = soup(html, "html.parser")
containers = soup.findAll("div",{"class":"vItem"})
print(len(containers))
for container in containers:
test = getattr(container, "data-id")
print(str(test))
with open('scraped.txt', 'w', encoding="utf-8") as file:
file.write(str(containers))
以下是每个容器的外观示例。你知道吗
div class="vItem Normal Container cItem" data-bestquality="0" data-category="Normal" data-collection="The Spectrum Collection" data-currency="0" data-custom="" data-exterior="" data-hashname="Spectrum%20Case" data-id="15631554103"
只需将带有
getattr()
的行更改为container.attrs["data-id"]
。这对我很有用。但在大多数尝试中,10秒的睡眠时间对我来说是不够的。你知道吗相关问题 更多 >
编程相关推荐