学习总结18

总结

1.css选择器

元素选择器
  • a
  • p
  • div
  • img
id选择器
  • #p1
class选择器
  • .c1

  • .c1.c2

  • p.c1

  • 我是段落

选择器1>选择器2
选择器1 选择器2

2.beautifulsoup4

from bs4 importBeautifulSoup

1,创建soup(soup对象代表整个网页)

soup = BeautifulSoup(‘网页源代码’,‘lxml’)

2,获取标签

soup.select(css选择器)

soup.select_one(css选择器)

标签对象.select(css选择器)

标签对象.select_one(css选择器)

3,获取标签内容、标签属性

标签对象.text

标签对象.attrs[属性名]

作业解析

import requests

from bs4 import BeautifulSoup

from re import sub, findall

import csv

def get_one_page(page):

​ “”“1,获取网页数据”“”

url = f’https://cd.zu.ke.com/zufang/pg{page}/#contentList’
response = requests.get(url)

“”“2,解析数据”“”

soup = BeautifulSoup(response.text,‘lxml’)

“”“获取每个房屋信息对应的div”“”

div_list = soup.select(‘.content__list–item’)
for div in div_list:
name = div.select_one(‘.twoline’).text.strip()
info = div.select_one(‘.content__list–item–des’).text.strip()
info = sub(r’\s+‘,’',info)
area = findall(r’\d+\.\d+m^2’,info)[0]
house_type = findall(r’\d+室\d+厅\d+卫’,info)[0]
address = div.select(‘.content__list–item–des>a’)
new_address = ‘-’.join([x.text for x in address])
price = div.select_one(‘.content__list–price’).text
w1.writerow([name, price, area, house_type, new_address])
print(‘------------------------一页获取完成--------------------’)
if __name__ == '__main__':
    w1 = csv.writer(open('files/贝壳租房.csv', 'w', encoding='utf-8', newline=''))
    w1.writerow(['房屋', '价格', '面积', '户型', '地址'])

    for x in range(1, 11):
        get_one_page(x)

selenium

from selenium.webdriver import Chrome

b = Chrome - 1,创建浏览器对象

b.get(‘https://movie.douban.com/top250?start=0&filter=’) - 2,打开网页

print(b.page_source) - 3,获取网页源代码

selenium获取多页数据翻页的方法:

from selenium.webdriver import Chrome

1,找到不同页的地址的变化规律,利用循环实现多页数据的请求

b = Chrome()

for x in range(0,76,25):

​ b.get(f’https://movie.douban.com/top250?start={x}&filter=')

​ print(b.page_source)

input()

2,点击翻页按钮,刷新页面内容,在刷新后获取网页源代码

from selenium.webdriver.common.by import By

b = Chrome()

b.get(‘https://movie.douban.com/top250?start=0&filter=’)

for _ in range(5):

​ print(b.page_source)

​ next = b.find_element(By.CLASS_NAME,‘next’)

​ next.click()

selenium获取标签

浏览器对象.b.find_element(获取方式,数据) - 返回符合条件的第一个标签,结果是标签对象

浏览器对象.b.find_elements(获取方式,数据) - 返回符合条件的所有标签,结果是列表,列表中的元素是标签对象

1,获取方式

By.ID - 通过ID属性值获取标签

By.CLASS_NAME - 通过class属性值获取标签

By.CSS_SELECTOR - 通过css选择器获取标签

By.LINK_TEXT - 通过a标签的标签内容获取标签

By.PARTIAL_LINK_TEXT - 通过a标签的标签内容获取标签

2.操作标签

1,输入框输入内容:输入框对应的标签.send_keys(内容)

2,点击标签:标签对象.click()

from selenium.webdriver.common.by import By

b = Chrome()

b.get(‘https://www.jd.com/’)

获取id属性值为key的标签

search = b.find_element(By.ID,‘key’)

search.send_keys(‘电脑\n’)

获取标签内容为“便宜包邮”的a标签

a1 = b.find_element(By.LINK_TEXT,‘便宜包邮’)

input(‘:’)

from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
import time

b = Chrome()
b.get(‘https://search.jd.com/Search?keyword=%E7%94%B5%E9%A5%AD%E9%94%85&enc=utf-8&wq=%E7%94%B5%E9%A5%AD%E9%94%85&pvid=058303d3cd58499fb8f5f3459afd4d6b’)
time.sleep(2)

用代码控制浏览器滚动

js中页面鼓动的代码:window.scrollBy(x方向的偏移量, y方向的偏移量)

b.execute_script(‘window.scrollBy(0, 8000)’)

for x in range(10):
b.execute_script(‘window.scrollBy(0, 800)’)
time.sleep(1)

time.sleep(2)
result = b.find_elements(By.CSS_SELECTOR, ‘#J_goodsList>ul>li’)
print(len(result))

input(‘结束:’)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值