python扇贝单词书,学习猿地-python 爬虫,爬取扇贝单词网单词

对于python爬虫并不是很熟悉,遇到下面的问题,希望知道的朋友可以给个解决问题的方向!

需要爬出扇贝单词网里面单词书的单词,现在对于没有目录的单词书可以爬出来,如果有一层目录就没办法了 需要手动到这层目录去,输入这层目录的url 才能把这个目录下的单词爬取出来。

下面是我的代码:

from bs4 import BeautifulSoup

from lxml import html

import xml

import re

import requests

file = open("vocabulay.txt", "w")

''' file = open("out.txt", "w") '''

pattern='([a-z,A-Z]*?)'

def spider(url):

f = requests.get(url)

soup = BeautifulSoup(f.content, "lxml")

word_list=soup.select('strong')

for word in word_list:

word=str(word)

word=re.findall(pattern,word)

if(len(word)!=0): #需要对list进行长度判断,否则访问word[0]会有问题

print(word[0])

file.writelines((word[0],"\n"))

url_list = [ "https://www.shanbay.com/wordlist/80770/87931/",

"https://www.shanbay.com/wordlist/80770/89734/"

]

unit = 1

for url in url_list:

file.write("\n#章节"+str(unit)+"\n")

unit+=1

for i in range(1,11):

url1=url+"?page="+str(i)

spider(url1)

file.close()

单词书的URL:https://www.shanbay.com/wordlist/80770

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值