【问题标题】:Extracting list of filenames from Github page with BeautifulSoup使用 BeautifulSoup 从 Github 页面中提取文件名列表
【发布时间】:2019-05-04 11:30:57
【问题描述】:

我正在用 python 编写一个程序,它会扫描我朋友和我自己的 GitHub 页面并显示所有上传文件的名称。我已经设法让它做到这一点。所有文件的名称都在标签下。问题是标签下还有其他随机文本,例如“通过上传添加文件”。我不希望这些出现。任何帮助,将不胜感激。亲切的问候。埃里克

我在打印最终结果时尝试了字符串剥离,但这仍然行不通。

这是我的代码:

import bs4
import requests
from bs4 import BeautifulSoup as soup
import lxml
import re
import time
import os
import webbrowser
import re

def webscrape():
    res = requests.get('https://github.com/Dukesan7/jerichson')
    type(res)
    soup = bs4.BeautifulSoup(res.text, 'lxml')
    type(soup)
    file = soup.select('a')
    file[1].getText()
    time.sleep(1)
    files = str(file)
    clean = re.compile('<.*?>')
    files = re.sub(clean, '', files)
    print (files)
    time.sleep(1)
    print ("1. Main Menu: 1")
    print ("2. exit?: 2")
    op = input (":")
    if op == "2":
        exit()
    else:
        MainMenu()

【问题讨论】:

  • 您想包括还是排除文件夹?例如谷歌2

标签: python python-3.x web-scraping beautifulsoup


【解决方案1】:

您的代码的简化版本:

from bs4 import BeautifulSoup as bs
import requests

res = requests.get('https://github.com/Dukesan7/jerichson')    
soup = bs(res.text, 'lxml')   
file = soup.find_all('a',class_="js-navigation-open")
for i in file:
    if '.' in i.text:
        print(i.text)

提供此输出:

21s.py
BVVVVV.exe
Calling Casino.py
Game Download Link.txt
Homework.py
Password Username System.py
Puzzle.txt
StopWatch.py
Voting ligitimacy system.py
Vowl counter.py
agenotage.py
coin.py
dice.py
explorer reset.bat
name and age dukesan.py
notification.pyw
reminder.py
win 21 game.py

这就是你要找的吗?

【讨论】:

  • 请注意,这不包括文件夹名称(不确定 OP 是否希望它们包括在内)。这也将排除任何不包含句点的文件名(可能发生)。
  • 确实;由于 OP 没有指出他想要的输出,我认为这已经足够接近了,如果有必要,他可以修改代码以满足他的需要。
【解决方案2】:

如果您在浏览器中使用检查器,您可以尝试查找所有文件/文件夹名称共有的类和/或标签。我发现它们都在 td 元素中,其类为 content,它有一个 tr 元素,类 js-navigation-item 为父类:

因此您可以在 BeautifulSoup 中使用以下选择器:tr.js-navigation-item &gt; td.content

请注意,您可以使用 elem.text 语法简单地提取 HTML 元素的文本。使用 Regex 不适合剥离 HTML 标签。

工作实施:

res = requests.get('https://github.com/Dukesan7/jerichson')
soup = bs4.BeautifulSoup(res.text, 'lxml')
files_list = soup.select('tr.js-navigation-item > td.content')
files_list_text = [f.text.strip() for f in files_list]
print(files_list_text)

输出:

['Google2', 'Maths Game', 'OpenMinecraft', '21s.py', 'BVVVVV.exe', 'Calling Casino.py', 'Game Download Link.txt', 'Homework.py', 'Password Username System.py', 'Puzzle.txt', 'StopWatch.py', 'Voting ligitimacy system.py', 'Vowl counter.py', 'agenotage.py', 'coin.py', 'dice.py', 'explorer reset.bat', 'name and age dukesan.py', 'notification.pyw', 'privilege_escalation', 'reminder.py', 'win 21 game.py']

【讨论】:

  • 谢谢。有效。只是想知道您是否可以告诉我这条线的确切作用,以便我更好地理解它。谢谢 files_list = soup.select('tr.js-navigation-item > td.content')
  • td.content 表示 td 元素,类为 content。类似地,tr.js-navigation-item 表示 tr 元素,类为 js-navigationelement1 &gt; element2 语法意味着,如果 element2 包含 element1,则仅选择它。请参阅developer.mozilla.org/en-US/docs/Web/CSS/Child_combinator 了解
  • 哦,好的。但是当我在那里检查页面时,我在内容附近的任何地方都看不到 tr?
  • 专门检查文件名以找到元素。或者使用 Ctrl+U 检查页面源并使用 Ctrl+F 搜索 tr。请参阅我在答案中添加的图片。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-14
  • 1970-01-01
  • 2019-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多