我正在尝试使用Firefox Webdriver , Selenium 和 Python
问题在于,当Selenium试图获取主页时,它会崩溃,并显示以下错误消息:
/usr/bin/python2.7 /home/ghasemi/PycharmProjects/phorcys_watcher/main.py
http://apps.evozi.com/apk-downloader/?id=com.instagram.android
Traceback (most recent call last):
File "/home/ghasemi/PycharmProjects/phorcys_watcher/main.py", line 7, in <module>
content = google_play_download("com.instagram.android")
File "/home/ghasemi/PycharmProjects/phorcys_watcher/collector.py", line 20, in google_play_download
browser.get("https://apps.evozi.com/apk-downloader/?id=" + app_page_id)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 268, in get
self.execute(Command.GET, {'url': url})
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 256, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Reached error page: about:neterror?e=unknownProtocolFound&u=httpss%3A//www.adnetworkperformance.com/script/java.php%3Foption%3Drotateur%26r%3D411313%26treqn%3D1025813717%26runauction%3D1%26crr%3D168ce9d76b1a6695b12e%2CwcwHrNzGnshFns2PnM3bbcwGW8xLz-mNycwuvZjurZja3MzJfMxG_9xMX4wYns7a2YxHvshBL9xe3shbjN2J7umN6umNm-mNuN2czNw723956800778f24b2db6%26rtid%3D595b6ecb8ac19%26cbrandom%3D0.7519066097934798%26cbtitle%3DAPK%2520Downloader%2520%255BLatest%255D%2520Download%2520Directly%2520%257C%2520Chrome%2520Extension%2520v3%2520%28Evozi%2520Official%29%26cbiframe%3D0%26cbWidth%3D1280%26cbHeight%3D717%26cbdescription%3DDownload%2520APKs%2520Directly%2520From%2520Google%2520Play%2520To%2520Your%2520Computer%2520With%2520APK%2520Downloader%2520Extension%2520For%2520Google%2520Chrome%26cbkeywords%3D%26cbref%3D&c=&f=regular&d=Firefox%20doesn%E2%80%99t%20know%20how%20to%20open%20this%20address%2C%20because%20one%20of%20the%20following%20protocols%20%28httpss%29%20isn%E2%80%99t%20associated%20with%20any%20program%20or%20is%20not%20allowed%20in%20this%20context.
此行引发异常: browser.get( “https://apps.evozi.com/apk-downloader/?id=com.instagram.android”)
如上所示,此错误的来源是selenium尝试下载的页面中的错误链接。我找到导致此错误的框架:
<iframe width="468" height="60" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" allowfullscreen="true" style="border: medium none; padding: 0; margin: 0;" sandbox="allow-scripts allow-forms allow-popups allow-popups-to-escape-sandbox allow-pointer-lock allow-same-origin" id="595b6e88086f8" frameborder="0" src="httpss://www.adnetworkperformance.com/script/java.php?option=rotateur&r=411313&treqn=501505383&runauction=1&crr=fc25086f39dc3c58bbdbGJTJyVGZh9Gbud3bk1yawFmRyUSbvNmLpp3b2VmLzBHchZkMlYkMlE0MlMHc0RHa2dfe473f7c304fd8fb65&rtid=595b6e88086f8&cbrandom=0.6676681852413189&cbtitle=APK%20Downloader%20%5BLatest%5D%20Download%20Directly%20%7C%20Chrome%20Extension%20v3%20(Evozi%20Official)&cbiframe=0&cbWidth=1522&cbHeight=741&cbdescription=Download%20APKs%20Directly%20From%20Google%20Play%20To%20Your%20Computer%20With%20APK%20Downloader%20Extension%20For%20Google%20Chrome&cbkeywords=&cbref=" scrolling="no"></iframe>
正如您所看到的,网页开发人员错误地放置了httpss
而不是https
(两次!)。
我该如何处理这个问题?
更新
我的刮刀:
import requests
from lxml import html
from pyvirtualdisplay import Display
from selenium import webdriver
def google_play_download(app_page_id):
browser = webdriver.Firefox()
browser.get("https://apps.evozi.com/apk-downloader/?id=" + app_page_id)
browser.find_element_by_css_selector(".btn.btn-primary.btn-lg.btn-block").click()
apk_link = browser.find_element_by_css_selector(".btn.btn-success.btn-block").get_attribute('href')
browser.quit()
for rnd in range(5):
resp = requests.get(apk_link)
if resp.headers['Content-Length'] == str(len(resp.content)):
return resp.content
if __name__ == "__main__":
content = google_play_download("com.instagram.android")
f = open('./file', 'wb')
f.write(content)
f.close()
[1]: https://apps.evozi.com/apk-downloader/
答案 0 :(得分:0)
一个解决方案是url-parsing funktion,你在driver.get(url)之前调用每个url
def url_parser(url):
if 'httpss' in url:
url = url.replace('httpss','https')
return url
然后你就这样使用它
url = url_parser(url)
driver.get(url)
答案 1 :(得分:0)
import requests
import time
from selenium import webdriver
def google_play_download(app_page_id):
browser = webdriver.Chrome()
browser.get("https://apps.evozi.com/apk-downloader/?id=" + app_page_id)
browser.find_element_by_css_selector(".btn.btn-primary.btn-lg.btn-block").click()
time.sleep(10)
apk_link = browser.find_element_by_css_selector(".btn.btn-success.btn-block").get_attribute('href')
browser.quit()
for rnd in range(5):
resp = requests.get(apk_link)
if resp.headers['Content-Length'] == str(len(resp.content)):
return resp.content
if __name__ == "__main__":
content = google_play_download("com.instagram.android")
f = open('file.apk', 'wb')
f.write(content)
f.close()
答案 2 :(得分:0)
The parser is not able to extract the proper url in this line apk_link = browser.find_element_by_css_selector(".btn.btn-success.btn-block").get_attribute('href')
As when you print the url its showing https://apps.evozi.com/apk-downloader/?id=com.instagram.android
Just change the line to
apk_link = browser.find_element_by_css_selector(".btn.btn-success.btn-block")
ele=apk_link.get_attribute('href')
for rnd in range(5):
resp = requests.get(ele)
if resp.headers['Content-Length'] == str(len(resp.content)):
return resp.content
The code will work without error