Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在Python中获取属性值_Python_Findall_Getattr - Fatal编程技术网

无法在Python中获取属性值

无法在Python中获取属性值,python,findall,getattr,Python,Findall,Getattr,我正在尝试为一个网站编写一个scraper,到目前为止,我能够获取我需要的一般信息,但是我试图从该信息中获取的特定属性值返回时没有返回,即使其中有明确的值。 在我尝试使用容器中每个容器的getattr来查找data-id的值之前,这一切都很正常。也许有更好的方法可以做到这一点,但我很难理解为什么找不到它 这就是我的代码的样子 from selenium import webdriver from selenium.webdriver.common.keys import Keys from bs

我正在尝试为一个网站编写一个scraper,到目前为止,我能够获取我需要的一般信息,但是我试图从该信息中获取的特定属性值返回时没有返回,即使其中有明确的值。 在我尝试使用容器中每个容器的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))
下面是每个容器的外观示例

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=“频谱采集”data currency=“0”data custom=”“data external=“”data hashname=“频谱%20Case”data id=“15631554103”


只需将带有
getattr()
的行更改为
container.attrs[“数据id”]
。这对我很有用。但是在大多数尝试中,10秒的睡眠时间对我来说是不够的

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))
data_ids = [] # Make a list to hold the data-id's

for container in containers:
    test = container.attrs["data-id"]
    data_ids.append(test) # add data-id to the list

    print(str(test))


with open('scraped.txt', 'w', encoding="utf-8") as file:
    for id in data_ids:
        file.write(str(id)+'\n') # write every data-id to a new line. 

只需将带有
getattr()
的行更改为
container.attrs[“数据id”]
。这对我很有用。但是在大多数尝试中,10秒的睡眠时间对我来说是不够的

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))
data_ids = [] # Make a list to hold the data-id's

for container in containers:
    test = container.attrs["data-id"]
    data_ids.append(test) # add data-id to the list

    print(str(test))


with open('scraped.txt', 'w', encoding="utf-8") as file:
    for id in data_ids:
        file.write(str(id)+'\n') # write every data-id to a new line. 

为什么要使用
getattr
??大概你是从什么地方学来的。就像我说的,我肯定有更好的方法,但就目前为止我的研究而言,这是我遇到的唯一一种不会让我困惑的方法。我假设它不是一个可靠的工具。为什么要使用
getattr
??大概你是从什么地方学来的。就像我说的,我肯定有更好的方法,但就目前为止我的研究而言,这是我遇到的唯一一种不会让我困惑的方法。我想这不是一个可靠的东西,我会试试的。我知道10秒只是一个占位符。最终,我希望它等到元素加载后再刮页面,但我还不完全确定如何做到这一点。这个站点的问题是,它有时加载速度快,有时加载速度慢。这可能是个愚蠢的问题,但我如何将存储在测试中的值转换成一个可以编写的全局变量呢?因为现在它只写其中一个,而不写其他的。参见上面的编辑。只需将
test
变量添加到列表中,最后将列表中的所有条目写入文件。感谢您的帮助!我会努力接受我刚刚学到的东西,继续前进。我会尝试一下。我知道10秒只是一个占位符。最终,我希望它等到元素加载后再刮页面,但我还不完全确定如何做到这一点。这个站点的问题是,它有时加载速度快,有时加载速度慢。这可能是个愚蠢的问题,但我如何将存储在测试中的值转换成一个可以编写的全局变量呢?因为现在它只写其中一个,而不写其他的。参见上面的编辑。只需将
test
变量添加到列表中,最后将列表中的所有条目写入文件。感谢您的帮助!我会努力接受我刚刚学到的东西,继续前进。