Python 请求 returns 我的状态 200,但没有执行预期的操作
Python requests returns me status 200, but does not perform the expected action
我正在尝试创建一个可以同时点赞 post 多个配置文件的应用程序。我正在尝试并行化登录,并且每个登录的用户都喜欢 posts 并行,如下所示。
with ProcessPoolExecutor() as exe:
bot = Insta()
results = []
for credential in credentials: # go through credentials, login in parallel.
results.append(
exe.submit(bot.login, credential) # Each login takes 15 sec
) # Add status of each process in a list
for result in as_completed(results): # When complete, I call like
if result.result() == 200:
with Pool(4) as p:
resp = p.map(bot.like, urls)
print(resp)
尽管还给我status_code 200,但当我看到post时,它并不被喜欢。
当我尝试为每次登录单独执行此操作时,returns 对我来说是一样的,但这次 post 真的很受欢迎。即:
bot = Insta()
resp = bot.login(credential)
if resp == 200:
with Pool(5) as p:
p.map(bot.like, urls)
谁能告诉我问题出在哪里?我想知道我是否做错了什么。我的点赞方法现在看起来像这样:
def like(self, url_post):
self._set_id_post(url_post) # id of post
resp = self.session.get(url_post)
self.session.headers = {'user-agent': self.user_agent}
self.session.headers.update({'Referer': url_post})
self.session.headers.update({'X-CSRFToken': resp.cookies['csrftoken']}, )
url = endpoints['like_url'] % self.post_id
time.sleep(random.gauss(6, 1.5))
response = self.session.post(url)
self.session.headers.update({'X-CSRFToken': resp.cookies['csrftoken']})
if response.status_code == 200:
return response.status_code
elif response.status_code == 403:
return response.status_code
elif response.status_code == 400:
return response.status_code
我通过为每个用户设置代理解决了这个问题。我不得不买它,因为 public 在我的情况下不起作用。但是对于那些在网络抓取方面有类似问题的人,除了社交网络,我将在这里插入我的代码之一,用于 return 可能有帮助的免费网站代理精英。
def free_proxy_list():
options = Options()
options.headless = True
driver = webdriver.Chrome(options=options)
driver.get('https://free-proxy-list.net/')
# Show only Elite Proxies
driver.find_element_by_xpath('//*[@id="proxylisttable"]/tfoot/tr/th[5]/select/option[3]').click()
# Show only SSl
driver.find_element_by_xpath('//*[@id="proxylisttable"]/tfoot/tr/th[7]/select/option[3]').click()
proxies = []
# Paginate 1
for i in range(1, 21):
xpath_ip = driver.find_element_by_xpath('//*[@id="proxylisttable"]/tbody/tr[%s]/td[1]' % i).text
xpath_port = driver.find_element_by_xpath('//*[@id="proxylisttable"]/tbody/tr[%s]/td[2]' % i).text
proxy = xpath_ip + ":" + xpath_port
proxies.append(proxy)
# Paginate 2
driver.find_element_by_xpath('//*[@id="proxylisttable_paginate"]/ul/li[4]/a').click()
try:
for i in range(1, 21):
xpath_ip = driver.find_element_by_xpath('//*[@id="proxylisttable"]/tbody/tr[%s]/td[1]' % i).text
xpath_port = driver.find_element_by_xpath('//*[@id="proxylisttable"]/tbody/tr[%s]/td[2]' % i).text
proxy = xpath_ip + ":" + xpath_port
proxies.append(proxy)
except NoSuchElementException:
return proxies
return proxies
我正在尝试创建一个可以同时点赞 post 多个配置文件的应用程序。我正在尝试并行化登录,并且每个登录的用户都喜欢 posts 并行,如下所示。
with ProcessPoolExecutor() as exe:
bot = Insta()
results = []
for credential in credentials: # go through credentials, login in parallel.
results.append(
exe.submit(bot.login, credential) # Each login takes 15 sec
) # Add status of each process in a list
for result in as_completed(results): # When complete, I call like
if result.result() == 200:
with Pool(4) as p:
resp = p.map(bot.like, urls)
print(resp)
尽管还给我status_code 200,但当我看到post时,它并不被喜欢。
当我尝试为每次登录单独执行此操作时,returns 对我来说是一样的,但这次 post 真的很受欢迎。即:
bot = Insta()
resp = bot.login(credential)
if resp == 200:
with Pool(5) as p:
p.map(bot.like, urls)
谁能告诉我问题出在哪里?我想知道我是否做错了什么。我的点赞方法现在看起来像这样:
def like(self, url_post):
self._set_id_post(url_post) # id of post
resp = self.session.get(url_post)
self.session.headers = {'user-agent': self.user_agent}
self.session.headers.update({'Referer': url_post})
self.session.headers.update({'X-CSRFToken': resp.cookies['csrftoken']}, )
url = endpoints['like_url'] % self.post_id
time.sleep(random.gauss(6, 1.5))
response = self.session.post(url)
self.session.headers.update({'X-CSRFToken': resp.cookies['csrftoken']})
if response.status_code == 200:
return response.status_code
elif response.status_code == 403:
return response.status_code
elif response.status_code == 400:
return response.status_code
我通过为每个用户设置代理解决了这个问题。我不得不买它,因为 public 在我的情况下不起作用。但是对于那些在网络抓取方面有类似问题的人,除了社交网络,我将在这里插入我的代码之一,用于 return 可能有帮助的免费网站代理精英。
def free_proxy_list():
options = Options()
options.headless = True
driver = webdriver.Chrome(options=options)
driver.get('https://free-proxy-list.net/')
# Show only Elite Proxies
driver.find_element_by_xpath('//*[@id="proxylisttable"]/tfoot/tr/th[5]/select/option[3]').click()
# Show only SSl
driver.find_element_by_xpath('//*[@id="proxylisttable"]/tfoot/tr/th[7]/select/option[3]').click()
proxies = []
# Paginate 1
for i in range(1, 21):
xpath_ip = driver.find_element_by_xpath('//*[@id="proxylisttable"]/tbody/tr[%s]/td[1]' % i).text
xpath_port = driver.find_element_by_xpath('//*[@id="proxylisttable"]/tbody/tr[%s]/td[2]' % i).text
proxy = xpath_ip + ":" + xpath_port
proxies.append(proxy)
# Paginate 2
driver.find_element_by_xpath('//*[@id="proxylisttable_paginate"]/ul/li[4]/a').click()
try:
for i in range(1, 21):
xpath_ip = driver.find_element_by_xpath('//*[@id="proxylisttable"]/tbody/tr[%s]/td[1]' % i).text
xpath_port = driver.find_element_by_xpath('//*[@id="proxylisttable"]/tbody/tr[%s]/td[2]' % i).text
proxy = xpath_ip + ":" + xpath_port
proxies.append(proxy)
except NoSuchElementException:
return proxies
return proxies