简体   繁体   English

如何在不实际打开浏览器的情况下使用Python向服务器发送URL请求(“不使用webbrowser模块”)?

[英]How can I send a URL request to the server with Python without actually opening my browser (“without using the webbrowser module”)?

I want to send this URL as a request to the server to change something on the website while I am logged in. The problem is, when I use mechanize or urllib2 to open the URL it doesn't change anything on the website. 我想将此URL作为请求发送给服务器,以便在我登录时更改网站上的内容。问题是,当我使用mechanize或urllib2打开URL时,它不会改变网站上的任何内容。 However, when I use the webbrowser module, it does change things on the website. 但是,当我使用webbrowser模块时,它确实改变了网站上的内容。 I want to do what the webbrowser module does, but WITHOUT opening the actual browser. 我想做webbrowser模块的功能,但没有打开实际的浏览器。 Is there a way to do this? 有没有办法做到这一点? And why are mechanize and urllib2 not working? 为什么机械化和urllib2不工作?

EDIT: What I mean by "changes to the website" is that I get these things called "Shares" and "Tickets" for information that I put on the website. 编辑:我的意思是“对网站的更改”是我得到的这些东西称为“分享”和“门票”,用于我放在网站上的信息。 My program finds accurate information (they will kick you out if its fake) and using a URL, "inserts" it into the website. 我的程序找到准确的信息(如果它是假的,它们会将你踢出去)并使用URL将其“插入”到网站中。

Example URL (all my others follow this format): 示例URL(我所有其他人都遵循此格式):

http://www.locationary.com/access/proxy.jsp?ACTION_TOKEN=proxy_jsp $JspView$SaveAction&inPlaceID=1020634218&xxx_c_1_f_987=http%3A%2F%2Fwww.yellowpages.com%2Fpittsburgh-pa%2Fmip%2Ffamily-dollar-store-1349194%3Flid%3D1349194 http://www.locationary.com/access/proxy.jsp?ACTION_TOKEN=proxy_jsp $ JspView $ SaveAction&inPlaceID = 1020634218&xxx_c_1_f_987 ​​= http%3A%2F%2Fwww.yellowpages.com%2Fpittsburgh-pa%2Fmip%2Ffamily-dollar-store- 1349194%3Flid%3D1349194

mechanize code: 机械化代码:

import mechanize
br = mechanize.Browser()
url = http://www.locationary.com/access/proxy.jsp?ACTION_TOKEN=proxy_jsp$JspView$SaveAction&inPlaceID=1020634218&xxx_c_1_f_987=http%3A%2F%2Fwww.yellowpages.com%2Fpittsburgh-pa%2Fmip%2Ffamily-dollar-store-1349194%3Flid%3D1349194
br.open(url)

urllib2 code: urllib2代码:

from urllib2 import urlopen
url = http://www.locationary.com/access/proxy.jsp?ACTION_TOKEN=proxy_jsp$JspView$SaveAction&inPlaceID=1020634218&xxx_c_1_f_987=http%3A%2F%2Fwww.yellowpages.com%2Fpittsburgh-pa%2Fmip%2Ffamily-dollar-store-1349194%3Flid%3D1349194
page = urllib2.urlopen(url)
page.read()

webbrowser code: webbrowser代码:

import webbrowser
url = http://www.locationary.com/access/proxy.jsp?ACTION_TOKEN=proxy_jsp$JspView$SaveAction&inPlaceID=1020634218&xxx_c_1_f_987=http%3A%2F%2Fwww.yellowpages.com%2Fpittsburgh-pa%2Fmip%2Ffamily-dollar-store-1349194%3Flid%3D1349194
webbrowser.open(url)

EDIT #2 I tried this code just now: 编辑#2我刚试过这段代码:

import urllib2
import urllib

def log_in():
    url = 'https://www.locationary.com/index.jsp?ACTION_TOKEN=tile_loginBar_jsp$JspView$LoginAction'
    values = {'inUserName' : 'me@gmail.com',
              'inUserPass' : 'myPass'}
    data = urllib.urlencode(values)
    req = urllib2.Request(url, data)
    req.add_header('Host', 'www.locationary.com')
    req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0')
    req.add_header('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
    req.add_header('Accept-Language', 'en-us,en;q=0.5')
    req.add_header('Accept-Encoding','gzip, deflate')
    req.add_header('Accept-Charset','ISO-8859-1,utf-8;q=0.7,*;q=0.7')
    req.add_header('Connection','keep-alive')
    req.add_header('Referer','http://www.locationary.com/')
    req.add_header('Cookie','site_version=REGULAR; __utma=47547066.1079503560.1321924193.1322707232.1324693472.36; __utmz=47547066.1321924193.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); nickname=jacob501; locaCountry=1033; locaState=1795; locaCity=Montreal; jforumUserId=1; PMS=1; TurnOFfTips=true; Locacookie=enable; __utma=47547066.1079503560.1321924193.1322707232.1324693472.36; __utmz=47547066.1321924193.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); nickname=jacob501; PMS=1; __utmb=47547066.15.10.1324693472; __utmc=47547066; JSESSIONID=DC7F5AB08264A51FBCDB836393CB16E7; PSESSIONID=28b334905ab6305f7a7fe051e83857bc280af1a9; __utmc=47547066; __utmb=47547066.15.10.1324693472; ACTION_RESULT_CODE=ACTION_RESULT_FAIL; ACTION_ERROR_TEXT=java.lang.NullPointerException')
    req.add_header('Content-Type','application/x-www-form-urlencoded')
    response = urllib2.urlopen(req)
    page = response.read()

url2 = 'http://www.locationary.com/access/proxy.jsp?ACTION_TOKEN=proxy_jsp$JspView$SaveAction&inPlaceID=1020634218&xxx_c_1_f_987=http%3A%2F%2Fwww.yellowpages.com%2Fpittsburgh-pa%2Fmip%2Ffamily-dollar-store-1349194%3Flid%3D1349194'

log_in()
response2 = urllib2.urlopen(url2)
page2 = response2.read()

but it didn't work. 但它不起作用。

EDIT 3: Code from tony that didn't work for me. 编辑3:来自tony的代码对我不起作用。

import urllib2
import urllib
import cookielib

data = urllib.urlencode({"inUserName":"MYUSERNAMESHOULDBEHERE", "inUserPass":"MYPASSWORDSHOULDBEHERE"})
jar = cookielib.FileCookieJar("cookies")
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
request = urllib2.Request("https://www.locationary.com/index.jsp?ACTION_TOKEN=tile_loginBar_jsp$JspView$LoginAction", data)
opener.open(request) 
url = "http://www.locationary.com/access/proxy.jsp?ACTION_TOKEN=proxy_jsp$JspView$SaveAction&inPlaceID=1012432546&xxx_c_1_f_987=http%3A%2F%2Fwww.yellowpages.com%2Fpittsburgh-pa%2Fmip%2Fdennys-13470813%3Flid%3D13470813"
anything = opener.open(url)
anything.read()

FINAL EDIT! 最终编辑! I finally got it to work using Tony's suggestions! 我终于使用Tony的建议让它工作了!

This is my final code that worked: 这是我的最终代码:

import urllib2
import urllib
import cookielib

data = urllib.urlencode({"inUserName":"myemail@gmail.com", "inUserPass":"mypassword"})
jar = cookielib.FileCookieJar("cookies")
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
opener.addheaders.append(('User-agent', 'Mozilla/4.0'))
opener.addheaders.append( ('Referer', 'http://www.hellboundhackers.org/index.php') )
opener.addheaders.append(('Cookie','site_version=REGULAR; __utma=47547066.912030359.1322003402.1324688192.1324930160.55; __utmz=47547066.1324655802.52.13.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=cache:dr23PN5fUj4J:www.locationary.com/%20locationary; nickname=jacob501; jforumUserId=1; PMS=1; locaCountry=1033; locaState=1786; locaCity=Vancouver; JSESSIONID=A8F241E1924CE7A25FAA8C5CA6597697; PSESSIONID=5c21c44245f978b917f17982c944a9ec2b5d2df5; Locacookie=enable; __utmb=47547066.5.10.1324930160; __utmc=47547066'))
request = urllib2.Request("https://www.locationary.com/index.jsp?ACTION_TOKEN=tile_loginBar_jsp$JspView$LoginAction", data)
response = opener.open(request) 
url = "http://www.locationary.com/"
anything = opener.open(url)
anything.read()

All I had to do was add the line 我所要做的就是添加线条

opener.addheaders.append(('Cookie','site_version=REGULAR; __utma=47547066.912030359.1322003402.1324688192.1324930160.55; __utmz= 

etc. etc. (the really long line of code, the cookie) 等等(真正长的代码行,cookie)

I also added a "Referer" and "User-Agent" header just in case. 我还添加了一个“Referer”和“User-Agent”标题以防万一。

Thanks tony!! 谢谢你!

First you should write url variable with quotes: 首先你应该用引号写url变量:

url = "http://www.locationary.com/access/proxy.jsp?ACTION_TOKEN=proxy_jsp$JspView$SaveAction&inPlaceID=1020634218&xxx_c_1_f_987=http%3A%2F%2Fwww.yellowpages.com%2Fpittsburgh-pa%2Fmip%2Ffamily-dollar-store-1349194%3Flid%3D1349194"

And if you wanna send request without opening browser you might use urllib, like you do. 如果您想在不打开浏览器的情况下发送请求,您可以使用urllib,就像您一样。

If you need authentication (seems like you do) you should send request for authentication, get cookies (use for it cookielib.FileCookieJar) and set them in opener. 如果您需要身份验证(似乎就是这样),您应该发送身份验证请求,获取cookie(使用它为cookielib.FileCookieJar)并在opener中设置它们。 Then you will be able to open pages and send requests. 然后,您将能够打开页面并发送请求。

Approximately you need something like: 大概你需要这样的东西:

data=urllib.urlencode({"login":"your login or whatever, "pass":"password}) # be aware you need to change "login" and "pass" to names of fields in form you have.
jar = cookielib.FileCookieJar("cookies")
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
request = urllib2.Request("url for authentication", data)
opener.open(request) # now you should be authorized and able to send any request like logged in user, using opener

url = "http://www.locationary.com/access/proxy.jsp?ACTION_TOKEN=proxy_jsp$JspView$SaveAction&inPlaceID=1020634218&xxx_c_1_f_987=http%3A%2F%2Fwww.yellowpages.com%2Fpittsburgh-pa%2Fmip%2Ffamily-dollar-store-1349194%3Flid%3D1349194"
anything = opener.open(url)
anything.read()
{"manifest":{"errorTimeout":0,"succeed":true,"errorCode":0,"serverVersion":"1.0","type":"locaaccess"},"saveResult":{"message":"You don't have permissions!","placeOpenedState":0,"isSucess":false}} 

I get that from putting your urllib in my browser. 我把你的urllib放在我的浏览器中就可以了。 You need to first authenticate yourself to the website I believe, then perform this command. 您需要首先向我认为的网站进行身份验证,然后执行此命令。 I can't give you instructions for how to log on to the site, but if you go to the log in page it may have a form that you can mimic with a url post via urllib2 我无法向您提供有关如何登录该站点的说明,但是如果您转到登录页面,它可能有一个表单,您可以通过urllib2模仿url帖子

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:在浏览器上打开本地 html 文件,而不使用 Webbrowser 或 Selenium - Python : Opening local html file on browser without using Webbrowser or Selenium 如何在无需实际打开浏览器的情况下使用 python 代码获取 chrome 版本? - How can I get chrome version using python code without having to actually open up a browser? 我们如何不使用Javascript将GET请求发送到python瓶服务器? - How can we send a GET request to a python bottle server without using Javascript? 不打开浏览器Python打开授权网址 - Open the authorization URL without opening browser Python 在没有使用Python的浏览器的情况下,如何从HTML表单发出POST请求? - How can I make a POST request from HTML form without browser using Python? 我可以在没有实际浏览器打开python的情况下使用Selenium和Scrapy - Can i use selenium with Scrapy without actual browser opening with python 如何在不使用请求模块和通过 xpath 的情况下使用 Python 提取重定向的 url? - How can I extract redirected url with Python without using requests module and via xpath? 如何在不安装的情况下测试我的python模块 - How can I test my python module without installing it 从python打开php url而不打开浏览器 - Opening a php url from python without opening the browser 如何向我的 PC 上的 HTML 文件发送请求,就好像它是使用 Python/Requests 的真实服务器一样? - How can I send a request to an HTML file on my PC as if it were a real server using Python/Requests?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM