小编Bra*_*itt的帖子

Scrapy 从任何网站获取所有链接

我有以下代码用于 Python 3 中的网络爬虫:

import requests
from bs4 import BeautifulSoup
import re

def get_links(link):

    return_links = []

    r = requests.get(link)

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

    if r.status_code != 200:
        print("Error. Something is wrong here")
    else:
        for link in soup.findAll('a', attrs={'href': re.compile("^http")}):
            return_links.append(link.get('href')))

def recursive_search(links)
    for i in links:
        links.append(get_links(i))
    recursive_search(links)


recursive_search(get_links("https://www.brandonskerritt.github.io"))
Run Code Online (Sandbox Code Playgroud)

该代码基本上从我的 GitHub 页面网站上获取所有链接,然后从这些链接中获取所有链接,依此类推,直到时间结束或发生错误。

我想在 Scrapy 中重新创建这段代码,这样它就可以服从 robots.txt 并成为一个更好的网络爬虫。我在网上进行了研究,我只能找到有关如何抓取特定域的教程/指南/stackoverflow/quora/博客文章(例如,allowed_domains=["google.com"])。我不想这样做。我想创建将递归抓取所有网站的代码。

这不是什么大问题,但所有的博客文章等都只展示了如何从特定网站获取链接(例如,他的链接可能在列表标签​​中)。我上面的代码适用于所有锚标记,无论它在哪个网站上运行。

我不想在野外使用它,我需要它用于演示目的,所以我不会突然因为过度的网络爬行而惹恼每个人。

任何帮助将不胜感激!

python web-crawler scrapy python-3.x

9
推荐指数
2
解决办法
1万
查看次数

摩尔斯电码到英文python3

我想使用Python 3+将莫尔斯电码转换为英语我已经设法使用此http://code.activestate.com/recipes/578407-simple-morse-code-translator-in-python/将英语转换为莫尔斯电码

但我想将莫尔斯电码转换为英语

我试图一次做一个字符,但问题是莫尔斯代码字母不像英文字母那样长,所以E是"." 而S是"......",我的问题是字典循环会找到"." 并将它与E匹配,所以我没有得到S我得到"EE E",而是试图通过检测空格并一次一个字来解决这个问题,但不是在单词中查找字母而是搜索整个单词字典我是Python和词典的新手,我不知道如何区分E"." 在搜索我的字典时,还有一个S"......"

这是我的代码

# defines the dictionary to convert morse to english
CODE_reversed = {'..-.': 'F', '-..-': 'X',
                 '.--.': 'P', '-': 'T', '..---': '2',
                 '....-': '4', '-----': '0', '--...': '7',
                 '...-': 'V', '-.-.': 'C', '.': 'E', '.---': 'J',
                 '---': 'O', '-.-': 'K', '----.': '9', '..': 'I',
                 '.-..': 'L', '.....': '5', '...--': '3', '-.--': 'Y',
                 '-....': '6', '.--': 'W', '....': 'H', '-.': 'N', '.-.': 'R',
                 '-...': 'B', '---..': '8', '--..': 'Z', '-..': 'D', …
Run Code Online (Sandbox Code Playgroud)

python dictionary morse-code

7
推荐指数
1
解决办法
1万
查看次数

如果String ONLY包含" - "或"."

我正在写一个小函数,它检查输入的字符串是否是莫尔斯码.该函数应该执行类似"If" - "或"的操作."仅在Inputted_string中:"但我似乎无法找到在Python3上执行唯一操作的方法.我实现这一目前的方式是非常混乱,而不是非常Pythonic

if "-" in message:
    # message might be morse code so check even more
        if "." in message:
            # Message IS morse code so return true
            return True
        else:
            # TODO you can use a REGEX for the below things
            if '--' in message:
                # if the messsage contains only hyphens, then check to see if
                # message contans hyphen only morse code by checking all hyphen
                # only morse code against message
                return True …
Run Code Online (Sandbox Code Playgroud)

python user-input morse-code

-1
推荐指数
1
解决办法
255
查看次数