我正在try 迭代URL列表,并使用请求和BeatifulSoup来提取每个URL的标题名称.

但我一直收到这样的错误:

请求.例外.无效架构:找不到"[‘https://reddit.com/?feed=home’,‘https://reddit.com/chunkCSS/CollectionCommentsPage~CommentsPage~CountryPage~Frontpage~GovernanceReleaseNotesModal~ModListing~Mod~e3d63e32.74eb929a3827c754ba25_.css’,‘https://reddit.com/chunkCSS/CountryPage~Frontpage~ModListing~Multireddit~ProfileComments~ProfileOverview~ProfilePosts~Subreddit.e72fce90a7f3165091b9_.css’,‘https://reddit.com/chunkCSS/Frontpage.85a25b7700617eafa94b_.css’,‘https://reddit.com/?feed=home’,‘https://reddit.com/r/popular/’,]的连接适配器"

《守则》:

pages = []
for admin_login_pages in domains:
    with open("urls.txt", "w") as f:
        f.write(admin_login_pages)
    if "admin" in admin_login_pages:
        if "login" in admin_login_pages:
            pages.append(admin_login_pages)
    with open("urls.txt", "r") as fread:
        url_list = [x.strip() for x in fread.readlines()]
        r = requests.get(str(url_list))
        soup = BeautifulSoup(r.content, 'html.parser')
        for title in soup.find_all('title'):
            print(f"{admin_login_pages} - {title.get_text()}")
if not pages:
    print(f"{Fore.RED} No admin or login pages Found")
else:
    for page_list in pages:
        print(f"{Fore.GREEN} {page_list}")

推荐答案

正如我在注释中所述,您将列表的字符串表示作为URL提供给请求.这是行不通的.相反,迭代url_list并分别向每个URL发出请求.

以下是略微重构的代码示例:

pages = []

with open("urls.txt", "r") as fread:
    url_list = [x.strip() for x in fread.readlines()]

with open("urls.txt", "w") as f:
    for admin_login_pages in domains:
        f.write(admin_login_pages)

        if "admin" in admin_login_pages and "login" in admin_login_pages:
            pages.append(admin_login_pages)

        for url in url_list:
            r = requests.get(url)
            soup = BeautifulSoup(r.content, "html.parser")

            title = soup.find("title")
            print(f"{admin_login_pages} - {title.get_text()}")

if not pages:
    print(f"{Fore.RED} No admin or login pages Found")
else:
    for page_list in pages:
        print(f"{Fore.GREEN} {page_list}")

Python-3.x相关问答推荐

visual studio代码窗口中未激活虚拟环境11

估计列表中连续对的数量

使用 iloc 或 loc 对多列进行过滤

Pandas教程:如何更新行内数值的位置

隐藏Cartopy中高纬度非矩形投影的右侧轴(纬度)标签

Django - ValueError:无法将字符串转换为浮点数:''

使用 python 查找标记的元素

Pygame 错误地渲染等距图像

如何准确测定cv2的结果.在BW/黑白图像中查找对象?

python用户输入5个偶数并打印最大的

如何在 Python 3 中通过 IP 获取 WhoIs 信息?

使用逗号时,除了处理程序中的语法无效

为什么`multiprocessing.Queue.get`这么慢?

如何找出从哪个模块导入名称?

类方法和实例方法同名

pdfminer python 3.5

如何为 anaconda python3 安装 gi 模块?

带有数千个逗号刻度标签的 MatPlotLib 美元符号

Python,Docker - ascii编解码器无法编码字符

Python 无法处理以 0 开头的数字字符串.为什么?