python吴枫千寻的_python爬虫第9关项目python之禅

要求:

爬取网页你好,蜘蛛侠!中的Python之禅中英文版本,并且打印。

你好,蜘蛛侠!是一个动态网页,URL:

https://localprod.pandateacher.com/python-manuscript/hello-spiderman/

题目要求:

需要通过两种方法获取:

只使用selenium

selenium与BeautifulSoup配合

方法一:使用selenium

from selenium import webdriver

import time

driver=webdriver.Chrome()

driver.get('https://localprod.pandateacher.com/python-manuscript/hello-spiderman/')

time.sleep(2)

teacher=driver.find_element_by_id('teacher')

teacher.send_keys('必须是吴枫呀')

assistant=driver.find_element_by_id('assistant')

assistant.send_keys('都喜欢')

button=driver.find_element_by_class_name('sub')

button.click()

time.sleep(2)

contents=driver.find_elements_by_class_name('content')

for content in contents:

print(content.find_element_by_tag_name('h1').text)

print(content.find_element_by_tag_name('p').text)

driver.close()

方法二:selenium与BeautifulSoup配合

from selenium import webdriver

import time

from bs4 import BeautifulSoup

driver=webdriver.Chrome()

driver.get('https://localprod.pandateacher.com/python-manuscript/hello-spiderman/')

time.sleep(2)

teacher=driver.find_element_by_id('teacher')

teacher.send_keys('必须是吴枫呀')

assistant=driver.find_element_by_id('assistant')

assistant.send_keys('都喜欢')

button=driver.find_element_by_class_name('sub')

button.click()

time.sleep(2)

#从这里开始获得网页源代码,然后再使用BeautifulSoup

pagesource=driver.page_source

soup=BeautifulSoup(pagesource,'html.parser')

contents=soup.find_all(class_='content')

for content in contents:

print(content.find('h1').text)

print(content.find('p').text)

driver.close()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值