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

标签 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]': '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])

所以这几乎完美地满足了我的需要,但随后出现了问题。我需要为我的项目抓取数据的网站每天都会更新 _vcnonce 变量。所以我的问题实际上归结为是否可以将特定的 html 字符串放入变量中。所以每次我运行代码时它都会自动更新。有点像这样

variable = w.e vcnonce attribute is
'_vcnonce': variable

或者类似的东西。这是一个项目,我需要获取信息,并且我能够将 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']    

关于python - 如何将特定的 html 属性拉入变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53180042/

相关文章:

python - 如何使用selenium在网页中搜索字典中的某个键,并在答案字段中填写相应的值? ( python , Selenium )

java - 如何在 selenium 3.141.59 中打开新选项卡

python - 从 S3 下载透明背景文件

python - 为什么 'python'和 `py`指向不同的用户站点

javascript - TypeError : name. toLowerCase 不是函数

html - 如何在Django2.0中使用html或css添加背景

selenium - org.openqa.selenium.remote.UnreachableBrowserException : Error communicating with the remote browser. 它可能与 ChromeDriver 和 Selenium 一起死亡

python - 我怎样才能动画化代数运算,最好是用Python

python - 如何使用 pysal 或 geopandas 并排绘制两张 map ?

javascript - Knockout JS valueUpdate 按键不适用于启用绑定(bind)