I used Selenium and PhantomJS hoping to get data from a website which using javascript to build the DOM.
The simple code below works, but it's not always valid. I meant that most of time it would return an empty website which didn't execute the javascript. It could seldom get the correct info I want.
from selenium import webdriver
from bs4 import BeautifulSoup
url = 'http://mall.pchome.com.tw/prod/QAAO6V-A9006XI59'
driver = webdriver.PhantomJS
driver.get(url)
print(driver.page_source, file=open('output.html','w'))
soup = BeautifulSoup(driver.page_source,"html5lib")
print(soup.select('#MetaDescription'))
It has a high probability to return an empty string :
[<meta content="" id="MetaDescription" name="description"/>]
Is the website server not allowing web crawlers? What can I do to fix my code?
What's more, all the info I need could be find in the <head>
's <meta>
tag.
(Like showing above, the data has an id MetaDescription
)
Or is there any simpler way to just get the data in <head>
tag?