Reputation: 3093
I have read Wait Until Page is Loaded, How to use Selenium Wait, Explicit Wait and other documentations to wait for a page to load and then start scraping. The wait successfully passes but I still get the same half/incomplete rendered HTML code.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
# prepare the option for the chrome driver
options = webdriver.ChromeOptions()
options.add_argument('headless')
# start chrome browser
browser = webdriver.Chrome(options=options,executable_path='C:/chromedriver_win32/chromedriver.exe')
browser.get('https://swappa.com/listing/view/LTNZ94446')
try:
WebDriverWait(browser, 30).until(EC.presence_of_element_located((By.ID, "wrap")))
print(browser.page_source)
except TimeoutException:
print("not found")
For this my output starts somewhere half-way rather than from <html>
at the top.
<div class="col-xs-6 col-sm-2 col-md-2">
<div class="img-container" style="margin-bottom: 15px;">
<a href="https://static.swappa.com/media/listing/LTNZ94446/mhhHypyw.jpg" class="lightbox">
<img class="img-responsive" src="https://static.swappa.com/images/cache/7b/67/7b679a1d89816bc341a802f19f661eac.jpg" alt="Listing Image" style="margin:0px 0px 0px 0px; ">
</a>
</div>
</div>
I am not sure where is it going wrong.
<div id="wrap">
) since it doesnt throw timeout errorIf there are ways using other libraries such as BeautifulSoup/URLLib/URLlib2/Scrapy, those would be helpful as well
Upvotes: 0
Views: 174
Reputation: 33384
You can use python requests module.
Code:
import requests
response=requests.get("https://swappa.com/listing/view/LTNZ94446")
if response.status_code==200:
print(response.text)
Upvotes: 0
Reputation: 12255
You can check if page fully loaded using JavaScript:
options = webdriver.ChromeOptions()
options.add_argument('headless')
# start chrome browser
browser = webdriver.Chrome(options=options)
browser.get('https://swappa.com/listing/view/LTNZ94446')
WebDriverWait(browser, 30).until(lambda d: d.execute_script(
'return ["complete", "interactive"].indexOf(document.readyState) != -1'))
# or use only complete
# WebDriverWait(browser, 30).until(lambda d: d.execute_script('return document.readyState == "complete"'))
print(browser.page_source)
Upvotes: 1