Python 如何将特定的html属性拉入变量

Python 如何将特定的html属性拉入变量,python,html,selenium,beautifulsoup,Python,Html,Selenium,Beautifulsoup,所以这个标题可能用词很糟糕,但我不知道该怎么说。所以我请求帮助使用beautifulsoup4来收集数据,有人很好心地帮了我 import requests from bs4 import BeautifulSoup import re #NJII params = { 'action': 'vc_get_vc_grid_data', 'tag': 'vc_basic_grid', 'data[page_id]': 26, 'data[shortcode_id

所以这个标题可能用词很糟糕,但我不知道该怎么说。所以我请求帮助使用beautifulsoup4来收集数据,有人很好心地帮了我

import requests
from bs4 import BeautifulSoup
import re

#NJII 
params = {
    'action': 'vc_get_vc_grid_data',
    'tag': 'vc_basic_grid',
    'data[page_id]': 26,
    'data[shortcode_id]': '1524685605316-ae64dc93-e23d-3',
    '_vcnonce': 'b9fb62cf69' #Need to update this somehow
}
dateList = []
urlList = []
url = 'http://njii.com/wp-admin/admin-ajax.php'
r = requests.get(url, params=params)
soup = BeautifulSoup(r.text, 'html.parser')
for div in soup.find_all('div', class_='vc_gitem-animated-block'):
    if re.search('2018', div.find('a')['href']):
        urlList.append(div.find('a')['href'])
        dateList.append(div.find('a')['href'])

#print(urlList)

count = 0;
while(count < len(dateList)):
    dateList[count] = re.search('[0-9]{4}/[0-9]{2}/[0-9]{2}', dateList[count])
    dateList[count] = dateList[count].group()
    count = count + 1

print(dateList[1])

或者类似的。这是一个项目,我需要获得信息,我能够使用selenium和beautifulsoup为其他网站。但不管怎样,这只会给我带来麻烦。所以我也尝试使用selenium,但它不起作用,我只是不确定即使使用selenium,我是否需要相同的参数。很抱歉问了这么长的问题。不确定什么是最好的方法。

您需要首先从事件页面获取值。然后,可以使用此选项提出进一步的请求。它作为属性包含在
div
元素中:

import requests
from bs4 import BeautifulSoup
import re

# First obtain the current nonce from the events page
r = requests.get("http://njii.com/events/")
soup = BeautifulSoup(r.content, 'html.parser')
vcnonce = soup.find('div', attrs={'data-vc-public-nonce':True})['data-vc-public-nonce']

#NJII 
params = {
    'action': 'vc_get_vc_grid_data',
    'tag': 'vc_basic_grid',
    'data[page_id]': 26,
    'data[shortcode_id]': '1524685605316-ae64dc93-e23d-3',
    '_vcnonce': vcnonce,
}
dateList = []
urlList = []

url = 'http://njii.com/wp-admin/admin-ajax.php'
r = requests.get(url, params=params)
soup = BeautifulSoup(r.text, 'html.parser')

for div in soup.find_all('div', class_='vc_gitem-animated-block'):
    if re.search('2018', div.find('a')['href']):
        urlList.append(div.find('a')['href'])
        dateList.append(div.find('a')['href'])

dates = [re.search('[0-9]{4}/[0-9]{2}/[0-9]{2}', date).group() for date in dateList]
print(dates)
这将为您提供如下输出:

['2018/11/01', '2018/10/22', '2018/10/09', '2018/10/09', '2018/10/03', '2018/09/27', '2018/09/21', '2018/09/13', '2018/09/12', '2018/08/24', '2018/08/20', '2018/08/02', '2018/07/27', '2018/07/11', '2018/07/06', '2018/06/21', '2018/06/08', '2018/05/24', '2018/05/17', '2018/05/14', '2018/05/04', '2018/04/20', '2018/03/28', '2018/03/26', '2018/03/23', '2018/03/22', '2018/03/15', '2018/03/15', '2018/02/27', '2018/02/19', '2018/01/18']    

那么问题是什么呢?为什么不能将变量作为键的值传递?“你从哪里得到这个值?”安德逊似乎需要在运行其余代码之前获得该键。我想知道他是否能在不登录任何东西的情况下得到那把钥匙。刚刚检查了页面,结果是空白的。如果
\vconce
值来自另一个请求,那么我猜OP正在搜索,但我不确定现在的问题是否广泛,我同意。。我访问了该网站,但无法进入登录区域,以查看是否有任何方法可以在控制台中获取此密钥。抱歉,回答太晚。该网站不会显示,因为这不是实际地址。它是njii.com/events/。当那个家伙帮助我时,他不得不把url放在那里,因为出于某种原因,它得到了回复。另外,我认为_vcnce来自另一个请求,因为它每天都在变化,我相信代码中的站点就是它发送请求的地方。当我有机会的时候,我会尽快检查你链接的请求会话内容。对不起,我的问题措辞不好。非常感谢!这正是我所需要的。我正试着做你刚才做的事,但我不知道该写什么。
['2018/11/01', '2018/10/22', '2018/10/09', '2018/10/09', '2018/10/03', '2018/09/27', '2018/09/21', '2018/09/13', '2018/09/12', '2018/08/24', '2018/08/20', '2018/08/02', '2018/07/27', '2018/07/11', '2018/07/06', '2018/06/21', '2018/06/08', '2018/05/24', '2018/05/17', '2018/05/14', '2018/05/04', '2018/04/20', '2018/03/28', '2018/03/26', '2018/03/23', '2018/03/22', '2018/03/15', '2018/03/15', '2018/02/27', '2018/02/19', '2018/01/18']