gpt4 book ai didi

python - 在 python 中浏览网站、抓取和发布

转载 作者:太空狗 更新时间:2023-10-30 01:22:30 25 4
gpt4 key购买 nike

stackoverflow 上已经有很多好的资源,但我仍然遇到问题。我访问过这些来源:

我正在尝试访问 http://www.latax.state.la.us/Menu_ParishTaxRolls/TaxRolls.aspx并选择教区。我相信这会强制发帖并允许我选择年份,再次发帖,并允许更多选择。我已经按照上述来源以几种不同的方式编写了我的脚本,但未能成功提交网站以允许我进入一年。

我当前的代码

import urllib
from bs4 import BeautifulSoup
import mechanize

headers = [
('Accept','text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
('Origin', 'http://www.indiapost.gov.in'),
('User-Agent', 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17'),
('Content-Type', 'application/x-www-form-urlencoded'),
('Referer', 'http://www.latax.state.la.us/Menu_ParishTaxRolls/TaxRolls.aspx'),
('Accept-Encoding', 'gzip,deflate,sdch'),
('Accept-Language', 'en-US,en;q=0.8'),
]

br = mechanize.Browser()
br.addheaders = headers

url = 'http://www.latax.state.la.us/Menu_ParishTaxRolls/TaxRolls.aspx'

response = br.open(url)
# first HTTP request without form data
soup = BeautifulSoup(response)
# parse and retrieve two vital form values
viewstate = soup.findAll("input", {"type": "hidden", "name": "__VIEWSTATE"})
eventvalidation = soup.findAll("input", {"type": "hidden", "name": "__EVENTVALIDATION"})

formData = (
('__EVENTVALIDATION', eventvalidation[0]['value']),
('__VIEWSTATE', viewstate[0]['value']),
('__VIEWSTATEENCRYPTED',''),

)



try:
fout = open('C:\\GIS\\tmp.htm', 'w')
except:
print('Could not open output file\n')

fout.writelines(response.readlines())
fout.close()

我也在 shell 中尝试过这个,我输入的内容加上我收到的内容(修改以减少批量)可以找到 http://pastebin.com/KAW5VtXp

无论如何,我尝试更改教区下拉列表中的值并发布我被带到网站管理员登录页面。

我的处理方式是否正确?任何想法都会非常有帮助。

谢谢!

最佳答案

我最终使用了 Selenium 。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://www.latax.state.la.us/Menu_ParishTaxRolls/TaxRolls.aspx")
elem = driver.find_element_by_name("ctl00$ContentPlaceHolderMain$ddParish")
elem.send_keys("TERREBONNE PARISH")
elem.send_keys(Keys.RETURN)

elem = driver.find_element_by_name("ctl00$ContentPlaceHolderMain$ddYear")
elem.send_keys("2013")
elem.send_keys(Keys.RETURN)

elem = driver.find_element_by_id("ctl00_ContentPlaceHolderMain_rbSearchField_1")
elem.click()

APN = 'APN # here'
elem = driver.find_element_by_name("ctl00$ContentPlaceHolderMain$txtSearch")
elem.send_keys(APN)
elem.send_keys(Keys.RETURN)

# Access the PDF
elem = driver.find_element_by_link_text('Generate Report')
elem.click()
elements = driver.find_elements_by_tag_name('a')
elements[1].click()

关于python - 在 python 中浏览网站、抓取和发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22235750/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com