菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
2663
3

python 爬虫,爬取扇贝单词网单词

原创
05/13 14:22
阅读数 1859

对于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='<strong>([a-z,A-Z]*?)</strong>'

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/200
2663 点赞
3 评论
收藏
为你推荐 换一批