python - 访问类范围python之外的变量
问题描述
我想在从页面上抓取它后更新__VIEWSTATE
和值。__EVENTVALIDATION
我正在浏览页面,并且需要在数据字典中引用一个唯一值__VIEWSTATE
和值。__EVENTVALIDATION
我想知道是否可以访问范围response
之外的变量,class
以便我可以将值发送到数据字典?
从输入导入计数器
import requests
from scrapy.selector import Selector
class Scraper:
val = ''
def bot(self, event_state='some-values', event_validate='some-valus'):
cookies = {}
headers = {}
counter = 0
while True:
data = {
'__EVENTTARGET': f'dgSearchResults$ctl24$ctl0{counter}',
'__VIEWSTATE': self.event_state,
'__VIEWSTATEGENERATOR': '11C1F95B',
'__EVENTVALIDATION': self.event_validate,
'cboType': 'RE',
'DateOfFilingFrom': '01/01/2021',
'DateOfFilingTo': '10/20/2021',
'cboPartyType': 'Decedent',
'cmdSearch': 'Search'
}
url = 'https://registers.maryland.gov/RowNetWeb/Estates/frmEstateSearch2.aspx'
r = requests.post(url=url, headers=headers, cookies=cookies, data=data)
response = Selector(text=r.text)
links = response.xpath('//a[contains(@href, "RecordId")]')
if links == []:
break
for link in links:
ids = link.xpath('.//@href').get()
print(ids)
counter += 1
event_state_value = response.xpath('//input[@name="__VIEWSTATE"]/@value').get()
event_validate_value = response.xpath('//input[@name="__EVENTVALIDATION"]/@value').get()
scraper = Scraper()
scraper.bot(event_state_value, event_validate_value)
解决方案
推荐阅读
- angular - return custom routes with angular and express
- java - how to switch from localhost to the internet in jsf
- android - Android 版本 >= 6.0 上不存在 String#value 字段
- elasticsearch - 麋鹿栈 GPG13
- php - 尝试选择字段而不选择第一个字符时出错
- docker - 使用 docker 在 Jenkins 中运行多个边车
- java - 播放框架 2.6.15(sbt 1.1.6)
- php - 指向 Elastic 的 AWS API 网关:访问密钥 PHP
- android - 在 exo 播放器中编辑视频帧
- docker - 无法让 dep 和 dockerize 在 docker-compose 中一起工作(但它们分开工作)。为什么?