selenium爬虫获取html
时间: 2023-08-11 16:47:02 浏览: 56
使用Selenium可以获取动态生成的HTML内容。以下是使用Selenium和Python获取HTML的示例代码:
```python
from selenium import webdriver
# 创建浏览器对象
driver = webdriver.Chrome()
# 访问网页
driver.get('https://www.example.com')
# 获取HTML内容
html = driver.page_source
# 关闭浏览器
driver.quit()
```
在上面的示例中,我们首先创建了一个Chrome浏览器对象,然后访问了一个网页。我们使用`driver.page_source`获取了页面的HTML内容,并将其存储在变量`html`中。最后,我们关闭了浏览器对象。
相关问题
selenium爬虫获取html中标签中属性
要获取HTML标签中的属性,可以使用Selenium中的`get_attribute()`方法。例如,如果要获取一个链接标签`<a>`中的`href`属性,可以使用以下代码:
```python
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://example.com")
link = driver.find_element_by_tag_name("a")
href = link.get_attribute("href")
print(href)
```
在上面的代码中,首先使用`find_element_by_tag_name()`方法获取第一个`<a>`标签,然后使用`get_attribute()`方法获取该标签的`href`属性。打印输出这个属性的值。
你可以根据你要获取的属性类型,将标签名和属性名替换为你需要的值。
selenium爬虫获取html中ul标签下所有a标签的href属性
要获取HTML中`ul`标签下所有`a`标签的`href`属性,可以使用Selenium的`find_elements_by_xpath()`方法和`get_attribute()`方法。以下是一个例子:
```python
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("http://example.com")
# 找到ul标签
ul = driver.find_element_by_xpath("//ul")
# 找到所有a标签
links = ul.find_elements_by_xpath(".//a")
# 循环遍历所有a标签,获取href属性
for link in links:
href = link.get_attribute("href")
print(href)
# 关闭浏览器
driver.quit()
```
在上面的代码中,我们首先使用`find_element_by_xpath()`方法找到`ul`标签,然后使用`.//a`语法查找所有子孙节点中的`a`标签。接着,使用`find_elements_by_xpath()`方法获取所有`a`标签,然后循环遍历每个标签,使用`get_attribute()`方法获取`href`属性的值,并打印输出。最后使用`quit()`方法关闭浏览器。
需要注意的是,如果要查找的`ul`标签和`a`标签具有特定的类名或ID,可以在XPath表达式中添加`@class`或`@id`属性,以缩小查找范围。例如,`//ul[@class='menu']//a`表示查找`class`属性为`menu`的`ul`标签下的所有`a`标签。